Jam*_*ago 9 html c# asp.net-mvc-2
我有一个表单发布到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>
<% } %>
Run Code Online (Sandbox Code Playgroud)
我的行动:
[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);
}
Run Code Online (Sandbox Code Playgroud)
Dar*_*rov 10
首先让您的select标签有效name.有效名称不能包含空格.
<select name="contentList">
Run Code Online (Sandbox Code Playgroud)
然后从表单参数集合中获取所选值:
var value = collection["contentList"];
Run Code Online (Sandbox Code Playgroud)
甚至更好:不要使用任何集合,使用与您的选择名称同名的操作参数,并保留默认模型绑定器填充它:
[HttpPost]
public ActionResult Index(string contentList)
{
// contentList will contain the selected value
return RedirectToAction("Details", contentList);
}
Run Code Online (Sandbox Code Playgroud)