我想使用 Html.DropDownList(string name, IEnumerable SelectList, Object htmlAttributes) 为我呈现一个带有预选值的选择列表。
我的选择列表在我 View 的模型对象中,所以我一直在编写以下代码:
<%= Html.DropDownList("aName", mySelectList, new { }) %>
这将呈现没有预选值的选择列表。
我发现的解决方法是将 SelectList 作为 ViewData 传递并执行以下操作:
在 Controller 中:
ViewData["TimeZones"] = mySelectList;
在 View 中:
<%= Html.DropDownList("TimeZones", null, new { }) %>
这样选择列表将使用预选值呈现,但是,我不想被迫将我的选择列表作为 View 数据传递。我究竟做错了什么?预先感谢您的帮助。
最佳答案
你可以简单地这样做(它有效):
<%: Html.DropDownList("DropdownName", new SelectList(ListItems, "Value", "Text", selectedItem), htmlAttributes)%>
如果这对您不起作用,请告诉我。
关于asp.net-mvc - HTML.DropDownList 不尊重预选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1935462/