Asp.net MVC, Html.DropDownList and Selected Value
I recently ran into the altogether common problem of the Html.DropDownList helper rendering a drop down list with no value selected. This is a major problem when editing data as by default, the first value is selected and saving would mean the first value is used.
There have been a few issues resulting in the same error. My issue was that I was setting the Name of the drop down list to be equal to the property on my model. I was using the Entity Framework, and had an Image class with a navigation property called Category. I was using this to render the ddl:
<%= Html.DropDownList("Category", (IEnumerable<SelectListItem>)ViewData["categories"])%>
In my controller, I was setting the ViewData like this:
this.ViewData["categories"] = new SelectList(db.CategorySet.ToList(), "CategoryId", "Title", img.CategoryReference.EntityKey);
Unfortunately, even though I had set the selected value (third parameter to the SelectList constructor), the ddl had no value selected.
The fix was quite simple:
<%= Html.DropDownList("CategoryId", (IEnumerable<SelectListItem>)ViewData["categories"])%>
I just changed the Name of the drop down and handled the assignment in the controller.
The reason behind this problem is that asp.net MVC first looks for a match between the name of the drop down and a property on the model. If there’s a match, the selected value of the SelectList is overridden. Changing the name of the drop down is all it takes to remedy the issue.
Hope that helps.