开发者

asp mvc how to passs multi selected value to controller

hi friend i want to pass multi selected value view to controller but my view pass only single selection my code is here public 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","Gan开发者_Python百科gtok","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);

view form code

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

another action method is

    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);
}

but this controller get only single value i can select multiple value


Change

public ActionResult candidatesearch(string location )

To

public ActionResult candidatesearch(string[] location )

Each item in the list should be an index in the array. Instead of your split you can then do:

foreach(var item in location)
{
    //do something with item
}

Hope this helps.


Instead of above answer you can use

       public ActionResult candidatesearch(FormCollection formValues )
         {

              string[] location = formvalues["Your_ListBox_value"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

          }

This is more useful if you want access many form values from your view in your controller . Use Firebug to see the generated IDs for your ListBox , Checkboxes and many more . If you find this answer useful then please mark it as an answer so that others will get benefit .

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜