Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp mvc how to passs multi selected value to controller

Tags:

c#

asp.net-mvc

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","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);

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

like image 940
sandy Avatar asked Jun 29 '11 10:06

sandy


People also ask

How do I pass multiple objects from controller view?

Using ViewData Using ViewData, we can pass any object from the controller to the view. The Type Conversion code is required when enumerating in the view. For the preceding example, we need to create ViewData to pass a list of teachers and students from the controller to the view.

Can we have multiple views for single controller?

You can / will absolutely have multiple Views from a Controller. Just think about creating a model for each view if you want to stick up with MVC pattern. Save this answer.


1 Answers

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.

like image 144
Chris Avatar answered Nov 14 '22 23:11

Chris