Skip to Navigation or Skip to Content

Steve Fenton
Author of The Reason Your Website Sucks

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

Feeds

RSS Feed

<< 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:

  1. 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).

  1. model.SomeOptions= new MultiSelectList(StoreItems, "Value", "Text", model.SelectedStuff);

And finally, this is how you drop it all onto the view.

  1. <%= Html.ListBox("SelectedStuff",model.SomeOptions, new { size = "8" }) %>