5

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?

1
  • This is still an issue i'm facing with MVC 1.0 Commented Jun 11, 2009 at 2:48

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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"]) %>

3 Comments

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.
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.
Yes I tried it. The first parameter is always the name of the rendered dropdown. Isn't it for you?
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.