views:

3223

answers:

3

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?

A: 

The following works for me (using MVC RC Refresh)

In the view:

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

So for your example that would probably be:

<%= Html.DropDownList("Id", (SelectList)ViewData["myList"]) %>
kay.herzam
Hi Kay, wouldn't that make the name and the id "Id" ? That would be wrong as I need the name and id to be 'myItem' as that's the id the form submission is looking for.
IainMH
Have you tried it? Check the rendered HTML as well.The way I understand it, in your controller, you define the value field to be the ID of your item model, and the name to be the value of the SelectListItem.So, by setting the first parameter in the dropdown to Id, you ask for this value field.
kay.herzam
Yes I tried it. The first parameter is always the name of the rendered dropdown. Isn't it for you?
IainMH
+3  A: 

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.

Troy
This works.. But why?????
IainMH
Accepting as answer as this works. But @Haacked will hopefully see it too.
IainMH
@Troy btw - I should be clear that the multiple question marks weren't directed at you by to the MVC overlords.
IainMH
A: 

I simply made my own drop-down helper. Perhaps not as efficient as the one built-in, but it does work.

Greg Ogle