C# asp mvc如何将多个选定值传递给控制器
嗨,朋友,我想将多个选择值视图传递给控制器,但我的视图只传递单个选择。我的代码在这里 公共行动结果索引() {C# asp mvc如何将多个选定值传递给控制器,c#,asp.net-mvc,C#,Asp.net Mvc,嗨,朋友,我想将多个选择值视图传递给控制器,但我的视图只传递单个选择。我的代码在这里 公共行动结果索引() { var location = new[]{ "select","Hyderabad","Tirupati","Vijayawada","Vishakapatnam","Itanagar","Dispur", "Guwahati","Raipur","Goa","Ahmedabad","Bharuch",
var location = new[]{
"select","Hyderabad","Tirupati","Vijayawada","Vishakapatnam","Itanagar","Dispur",
"Guwahati","Raipur","Goa","Ahmedabad","Bharuch",
"Godhra","Jamnagar","Kheda","Rajkot","Surat","Vadodara","Faridabad","Gurgaon","Shimla","Dra
ss","Hiranagar","Poonch","Dhanbad","Ranchi","Bangalore","Hassan","Hubli","Karwar","Mangalore","Mysore","Udupi","Alappuzha","Kannur",
"Kochi","Kollam","Kottayam","Kozhikode","Palakkad","Pathanamthitta","Thiruvananthapuram","Thrissur","Bhopal","Indore","Aurangabad",
"Mumbai","Nagpur","Nasik","Pune","Thane",
"Imphal","Shillong","Aizawl","Kohima","Bhubaneswar","Rourkela","Amritsar","Chandigarh","Jalandhar","Ludhiana","Jaipur","Jodhpur","Udaipur","Gangtok","Chennai","Coimbatore","Karur","Madurai","Thirunelveli","Trichi","Agartala","Delhi" ,"Pondicherry","Allahabad","Lucknow","Varanasi","Kanpur","Durgapur","Kharagpur","Kolkata"
};
var Location = from d in location orderby d ascending select d;
ViewData["Location"] = new MultiSelectList(Location);
查看表单代码
<% using(Html.BeginForm("candidatesearch","Process",FormMethod.Post)){ %>
<%:Html.ListBox("location", ViewData["Location"] as MultiSelectList)%><br />
<input id="location" type="submit" value ="Search"/>
另一种行动方法是
public ActionResult candidatesearch(string location )
{
string rg = "";
string[] candidatelocation = location.Split(',');
for (int i = 0; i <= candidatelocation.Length;i++ )
{
rg=rg+candidatelocation[i];
}
Response.Write(rg);
}
public ActionResult候选搜索(字符串位置)
{
字符串rg=“”;
字符串[]candidatelocation=location.Split(',');
对于(inti=0;i变化
public ActionResult candidatesearch(string location )
到
列表中的每个项目都应该是数组中的索引。然后可以执行以下操作,而不是拆分:
foreach(var item in location)
{
//do something with item
}
希望这有帮助。您可以用上面的答案代替
public ActionResult candidatesearch(FormCollection formValues )
{
string[] location = formvalues["Your_ListBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}
如果希望从控制器的视图中访问多个表单值,则此选项更为有用。请使用Firebug查看列表框、复选框等的生成ID。如果您发现此答案有用,请将其标记为答案,以便其他人从中受益。请记住选择“作为答案:)
public ActionResult candidatesearch(FormCollection formValues )
{
string[] location = formvalues["Your_ListBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}