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
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With