How To Handle Multiple Select Lists In ASP NET MVC
You Are Here: Home » Blog » How To Handle Multiple Select Lists In ASP NET MVC
<< January | February | March >>
Sunday, 21st February 2010
I recently wrote a jQuery plugin for multiple select lists. This is because I was creating a two-sided multi-select in an ASP.NET MVC application.
I thought it would be useful for people if I posted how I've handled the binding of the multi-select list in the ASP.NET MVC application as it is just slightly trickier than binding a normal drop-down list.
Add an IEnumerable<string> property to your model to hold the selected values:
public IEnumerable<string> SelectedStuff { get; set; }
And on your controller, you can shove this property into the MultiSelectList. I'm putting this into the view data as I don't really want to weigh down my model with lots of available selections. (In this example, I have a List<SelectListItem> that I am passing in to create the MultiSelectList).
And finally, this is how you drop it all onto the view.
<%= Html.ListBox("SelectedStuff",model.SomeOptions, new { size = "8" }) %>