ASP.Net MVC3 Dropdownlist和传递数据

时间:2011-07-26 16:19:46

标签: c# asp.net-mvc-3

我有这个控制器

    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从视图传递回控制器。任何指针将不胜感激。我查看了其他帖子,但似乎无法找到有助于此的解决方案。

提前感谢您的时间。

1 个答案:

答案 0 :(得分:1)

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

在填充列表的视图中:

<% 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();
}