【发布时间】:2014-11-13 09:45:45
【问题描述】:
我正在使用 MVC 5。
我的 ViewBag 列表为
ViewBag.TitleList = new SelectList((new string[] { "Mr", "Miss", "Ms", "Mrs" }), contact.Title);
//contact.Title has selected value.
然后我尝试将数组转换为SelectListItem ( to no avail)
在视图上看起来像
@Html.DropDownListFor(model => model.Title, ViewBag.TitleList as SelectList, "Select")
我也试过
@Html.DropDownList("Title", ViewBag.TitleList as SelectList, "Select")
列表加载成功,但Selected Value is Not selected。
如何解决这个问题?
更新 罪魁祸首是与我的 model.Title 匹配的 ViewBag.Title。将我的模型属性重命名为其他东西,它工作。啊!
【问题讨论】:
-
使用
@Html.DropDownListFor()如果model.Title的值是(比如)“Miss”,那么将选择第二个选项(您不需要SelectList构造函数中的第二个参数)。检查Title的值是否与选项值之一完全匹配。 -
是的,标题中的值与列表中的值匹配
-
可能是该帖子的副本。 [可能重复][1][1]:stackoverflow.com/questions/5858679/…
-
感谢您的更新。我花了 30 分钟想知道为什么我的标题下拉列表没有被选中。在今天之前我从来不知道这是一个问题 - 谢谢!
标签: c# asp.net-mvc asp.net-mvc-5 html.dropdownlistfor selectlist