- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我的 Controller 中,我有以下内容:
ViewData["myList"] =
new SelectList(itemRepository.GetAll(), "Id", "Name", currentItem.Id);
在我看来:
<%= Html.DropDownList("myItem", (SelectList)ViewData["myList"])%>
呈现的下拉列表应该预先选择了 Id 为 currentItem.Id 的项目,但事实并非如此。未选择任何内容,因此默认为第一个。
这在我更新到 RC/RC(刷新)之前有效。有什么想法吗?
最佳答案
如果您将 ViewData 中的键名称与 View 中表单字段的名称相匹配,HtmlHelpers 将设计为基于该键从 ViewData 中隐式提取。我建议将您的 View 代码更改为:
<%= Html.DropDownList("myList") %>
HtmlHelpers 似乎在以这种方式使用它们时效果最好(尽管这并不总是可能的)。
更新:
为了进一步说明这似乎有效而其他方法无效的原因,我深入研究了 SelectExtensions.cs 的代码...
无论您如何调用 DropDownList,最终呈现实际 HTML 的是私有(private)方法 SelectInternal。 SelectInternal 的签名如下所示:
SelectInternal( string optionLabel, string name, IEnumerable<SelectListItem> selectList, bool usedViewData, bool allowMultiple, IDictionary<string,object> htmlAttributes )
这是 DropDownList 的两种用法所采用的路径:
DropDownList("myList")
DropDownList( string name ) ->
SelectInternal( null, name, htmlHelper.GetSelectData(name), true, false, null )
DropDownList("myItem",(SelectList)ViewData["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 )
所以归根结底,这两条路径之间的区别在于,有效的方式将 true 传递给 SelectInternal 的 usedViewData 参数,而无效的方式t 工作通过 false。
在我看来,当 usedViewData 为 false 时,SelectInternal 内部某处可能存在错误。
关于c# - ASP.NET MVC RC(刷新)中的 Html.DropDownList 未预选项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/589935/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!