[英]Get the selected drop down list value from a FormCollection in MVC
我有一個表單發布到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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.