Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
426 views
in Technique[技术] by (71.8m points)

c# - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...