Thursday, May 7, 2009

Setting up a dropdown list in ASP.NET MVC

When adding a dropdownlist to a view in MVC there are two approaches that I've taken so far. Both of these approaches involve the same snippet of code in the view:

<% = Html.DropDownList("ModelData")%>

ModelData is the item in the ViewData[] collection that holds your list. You set this value in the Controller.

The Controller code creates or fetches a collection of items for the drop down and then assigns them to an item in the ViewData collection:

ViewData["ModelData"] = dropDownList;

The drop down list needs to be an IEnumerable. I have taken two approaches to creating this. The first is to create a dictionary of int's and string's which is the logical backing to a dropdown list.

Dictionary<int, string> dictionaryList = FetchDictionaryOfDropDownListItems();
ViewData["ModelData"] = new SelectList(dictionaryList, "Key", "Value");

Note that if you don't supply the "Key" and "Value" fields for the second and third parameters of the SelectList() constructor then both the integer (key) values and the string values from the Dictionary will appear in your dropdown list. By supplying these values you are telling the SelectList which are the values in your dropdown list and which is the data to display.

The second approach was to create a list of SelectListItems:

IEnumerable<SelectListItem> dropDownList = new listOfObjects.Select(a => new SelectListItem { Text = a.MyText, Value = a.MyValue });
ViewData["ModelData"] = dropDownList;

I'm using LINQ on an IEnumerable that I called "listOfObjects" which has objects that have at the minimum a MyText and MyValue public property. I'm assigning these to the SelectListItem's Text and Value properties. The Text property is what you'll see in the dropdown list and the Value is the backing value for each item, usually an ordered list of integers.

8 comments:

  1. Thanks for the article. Please also check out this article for other methods to bind dropdownlist.
    altafkhatri.com/.../How_to_bind_ILi

    ReplyDelete
  2. Thank you so much for this article! It was very helpful!

    ReplyDelete
  3. What about your model?
    Where did you declare "Select" and "dropDownList" to include them in the model?

    ReplyDelete
  4. Instead of passing a model to the view with a property that holds the values for a drop down list I set the data in the ViewData["ModelData"] collection. This is then accessed from the view to retrieve the data. The SelectList is a .NET type.

    ReplyDelete
  5. Can u plz provide a source code for this.

    ReplyDelete
  6. Crap article. STOP using ViewData["whatever"], it is not the right design pattern for proper SoC. Re-write a poper torial that is not duct tape code and can be used by enterprise.

    ReplyDelete
  7. Hi,
    I am looking to bind Enum to drop down list in ASP.NET MVC 3.
    I found this page, can somebody tell me if this is a good way or not. fahadash.rollr.com/.../aspnet-mvc-bind

    ReplyDelete