我有这个 Controller
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();
}
以及相应的 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 从 View 传递回 Controller 。任何指针将不胜感激。我看过其他帖子,但似乎无法找到有助于解决此问题的解决方案。
提前感谢您的宝贵时间。
最佳答案
您可以创建一个接受 FormCollection 参数的 ActionResult,以便从您 View 中的控件获取值,如下所示:
在 View 中填充列表:
<% using (Html.BeginForm("MyActionButton", "Home")) { %>
<%= Html.DropDownList("MyList",(SelectList)ViewData["testItems"], "None") %>
<input type="submit" value="send" />
<% } %>
在Server端获取值:
public ActionResult MyActionButton(FormCollection collection)
{
string value = collection["MyList"];
return View();
}
关于c# - ASP.Net MVC3 下拉列表和传递数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6833257/