我有一个使用 MVC 发布到操作的表单。我想从操作中的 FormCollection 中提取选定的下拉列表项。我该怎么做?
我的 HTML 表单:
<% using (Html.BeginForm())
{%>
<select name="Content List">
<% foreach (String name in (ViewData["names"] as IQueryable<String>)) { %>
<option value="<%= name %>"><%= name%></option>
<% } %>
</select>
<p><input type="submit" value="Save" /></p>
<% } %>
我的行动:
[HttpPost]
public ActionResult Index(FormCollection collection)
{
//how do I get the selected drop down list value?
String name = collection.AllKeys.Single();
return RedirectToAction("Details", name);
}
首先给你的select
标记一个有效的name
。有效名称不能包含空格。
<select name="contentList">
然后从表单参数集合中获取选定的值:
var value = collection["contentList"];
或者甚至更好:不使用任何集合,使用与您的选择名称同名的操作参数,并保留默认模型绑定器填充它:
[HttpPost]
public ActionResult Index(string contentList)
{
// contentList will contain the selected value
return RedirectToAction("Details", contentList);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)