asp mvc如何将多个选定值传递给控制器

san*_*ndy 4 c# asp.net-mvc

嗨朋友我想将多个选定的值视图传递给控制器​​但我的视图只传递单个选择我的代码在这里公共ActionResult Index(){

        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);
Run Code Online (Sandbox Code Playgroud)

查看表单代码

<% using(Html.BeginForm("candidatesearch","Process",FormMethod.Post)){ %>
<%:Html.ListBox("location", ViewData["Location"] as MultiSelectList)%><br />
<input id="location" type="submit" value ="Search"/>
Run Code Online (Sandbox Code Playgroud)

另一种行动方法是

    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);
}
Run Code Online (Sandbox Code Playgroud)

但是这个控制器只获得单个值我可以选择多个值

Chr*_*ris 7

更改

public ActionResult candidatesearch(string location )
Run Code Online (Sandbox Code Playgroud)

public ActionResult candidatesearch(string[] location )
Run Code Online (Sandbox Code Playgroud)

列表中的每个项目都应该是数组中的索引.而不是你的分裂你可以做:

foreach(var item in location)
{
    //do something with item
}
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.