Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item

In my controller, I have the following:

ViewData["myList"] = 
   new SelectList(itemRepository.GetAll(), "Id", "Name", currentItem.Id);

And in the view I have:

<%= Html.DropDownList("myItem", (SelectList)ViewData["myList"])%>

The rendered drop down list should have the item with the Id of currentItem.Id pre-selected but it doesn't. Nothing is selected so it defaults to the first one.

This worked before I updated to the RC/RC(refresh). Any ideas?

like image 243
Iain Holder Avatar asked Feb 26 '09 10:02

Iain Holder


1 Answers

If you match the name of the key in ViewData to the name of the form field on the view, the HtmlHelpers are designed to implicitly pull from the ViewData based on that key. I would suggest changing your view code to:

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

The HtmlHelpers seem to work best when using them in this fashion (though that is not always possible).

Update:

To expand upon the reason why this seems to work while the other methods don't, I dove into the code for SelectExtensions.cs...

However you call DropDownList, the private method SelectInternal is what eventually renders the actual HTML. The signature for SelectInternal looks like:

SelectInternal( string optionLabel, string name, IEnumerable<SelectListItem> selectList, bool usedViewData, bool allowMultiple, IDictionary<string,object> htmlAttributes )

Here is the path the two usages of DropDownList take:

DropDownList("myList")

DropDownList( string name ) ->
SelectInternal( null, name, htmlHelper.GetSelectData(name), true, false, null )

DropDownList("myItem",(SelectList)ViewData["myList"]) DropDown

List( string name, IEnumerable<SelectListItem> selectList ) ->
DropDownList( name, selectList, null /* object, htmlAttributes */ ) ->
DropDownList( name, selectList, new RouteValueDictionary(htmlAttributes) ) ->
SelectInternal( null, name, selectList, false, false, htmlAttributes )

So at the end of the day, the difference between the two paths is that the way that works passes true into SelectInternal's usedViewData parameter, while the way that doesn't work passes false.

It seems likely to me that there is a bug somewhere inside SelectInternal when usedViewData is false.

like image 124
Troy Avatar answered Nov 04 '22 10:11

Troy