如果您将ViewData中的键的名称与视图上的表单字段的名称相匹配,HtmlHelpers被设计为隐式地从ViewData中取出,基于那把钥匙。我建议改变你的视图代码:
<%= Html.DropDownList("myList") %>
的HtmlHelpers似乎以这种方式使用的时候效果最好(虽然这并不总是可能的)。
更新:
扩大后为什么这似乎而其他方法不,我一头扎进了SelectExtensions.cs代码工作的原因...
但是你打电话的DropDownList ,私有方法SelectInternal最终呈现实际的HTML。对于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”]) 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)
因此,在这一天结束的时候,这两个路径之间的区别在于工作方式通过真正到SelectInternal的usedViewData参数,而不能上班的路上经过假。
这很可能,我认为有一个bug某处内SelectInternal时usedViewData是假。
这仍然是我与MVC 1.0面临的问题 – 2009-06-11 02:48:15