ASP.Net MVC3下拉列表和传递数据

本文关键字:数据 下拉列表 Net MVC3 ASP | 更新日期: 2023-09-27 18:03:40

我有这个控制器

    public ActionResult Index()
    {        
        IList<Partner> p = r.ListPartners();            
        ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name");
        return View();
    }
    //
    // POST: /Epub/
    [HttpPost]
    public ActionResult Index(IEnumerable<HttpPostedFileBase> fileUpload)
    {            
        IList<Partner> p = r.ListPartners();
        ViewBag.Partners = new SelectList(p.AsEnumerable(), "PartnerID", "Name");          
        int count = 0;
        for (int i = 0; i < fileUpload.Count(); i++)
        {
            if (fileUpload.ElementAt(i) != null)
            {
                count++;
                var file = fileUpload.ElementAt(i);
                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    // need to modify this for saving the files to the server
                    var path = Path.Combine(Server.MapPath("/App_Data/uploads"), Guid.NewGuid() + "-" + fileName);
                    file.SaveAs(path);
                }
            }
        }
        if (count == 0)
        {
            ModelState.AddModelError("", "You must upload at least one file!");
        }
        return View();
    }

和相应的视图

        @{
    ViewBag.Title = "Index";
}
<h2>You are in the epub portal!</h2>
@Html.Partial("_Download")

@model IEnumerable<EpubsLibrary.Models.Partner>
@{            
    @Html.DropDownList("PartnerID", (IEnumerable<SelectListItem>)ViewBag.Partners)
}

我试图弄清楚如何将选定的PartnerID从视图传递回控制器。任何提示将不胜感激。我看了其他的帖子,但似乎不能找到一个解决方案,将有助于这一点。

提前感谢您的时间。

ASP.Net MVC3下拉列表和传递数据

你可以创建一个接受FormCollection参数的ActionResult,以便从视图中的控件获取值,如下所示:

在视图中填充列表:

<% using (Html.BeginForm("MyActionButton", "Home")) { %>
   <%= Html.DropDownList("MyList",(SelectList)ViewData["testItems"], "None") %>
   <input type="submit" value="send" />
<% } %>

在服务器端获取值:

public ActionResult MyActionButton(FormCollection collection)
{
    string value = collection["MyList"];
    return View();
}