Article information
This post was published on The C#rypt by mhenrixon on 31 January 2009 at 14:17 and is tagged MVC.
Problem
The author explains that Html.DropDownList was not working and that clear information about the problem was difficult to find.
Solution
The view should use the helper with one parameter: <%= Html.DropDownList("CustomerID") %>
- Build the SelectList in the controller to keep logic out of the view.
- Use new SelectList(customers, "ID", "Name") so ID is the value field and Name is the display field.
- Store the result in ViewData["CustomerID"] and let MVC render the dropdown.
The example controller action loads an invoice, retrieves customers, creates the SelectList from that data, and passes it to the view through ViewData.