Html.DropDownList在ASP.NET MVC RC(刷新)不是pre-选择项目 [英] Html.DropDownList in ASP.NET MVC RC (refresh) not pre-selecting item

查看:90
本文介绍了Html.DropDownList在ASP.NET MVC RC(刷新)不是pre-选择项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的控制器,我有以下几点:

In my controller, I have the following:

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

和在视图中我有:

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

渲染的下拉列表中的应该的有ID的项目currentItem.Id pre-选择,但事实并非如此。没有被选择则默认为第一个。

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.

这工作之前,我更新到RC / RC(刷新)。任何想法?

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

推荐答案

如果你匹配的ViewData到视图上的表单字段的名称键的名称,HtmlHelpers旨在含蓄地从基于ViewData的拉这把钥匙。我建议改变你的看法code到:

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

的HtmlHelpers似乎当以这种方式使用它们的工作最好(尽管这并不总是可能的)。

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

更新:

要在扩大的原因为什么这似乎工作,而其他方法不这样做,我一头扎进了$ C $下SelectExtensions.cs ...

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

不过你打电话的DropDownList,私有方法SelectInternal就是最终呈现实际的HTML。对于SelectInternal签名是这样的:

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 )

下面是DropDownList中的两种用法走路径:

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的)计算机[myList上]) 下拉

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 )

因此​​,在这一天结束的时候,这两个路径之间的不同在于工作方式通过的真正的成SelectInternal的 usedViewData 参数,而那并不是'的方式科技工作通过的的。

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.

似乎有可能,我认为有一个bug某处SelectInternal当 usedViewData 的。

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

这篇关于Html.DropDownList在ASP.NET MVC RC(刷新)不是pre-选择项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆