结论:
- 无法绑定默认值可能是微软MVC的一个BUG
触发条件:
整个Controller中ViewBag或者ViewData构造的参数别名有与DropDownListFor和DropDownList构造的页面标签名字相同时出现。
注意是整个Controller中ViewBag和ViewData对象的所有参数,并不是赋值为SelectList的参数
例子:
- 错误:不能如愿选中默认值
@Html.DropDownList("Filed1", ViewBag.Filed1 as SelectList, new { @class = "form-control" })
@Html.DropDownListFor(model=>model.Filed1, ViewBag.Filed1 as SelectList, new { @class = "form-control" })
- 正确:可以如愿选中默认值
@Html.DropDownList("Filed1", ViewBag.temp as SelectList, new { @class = "form-control" })
@Html.DropDownListFor(model=>model.Filed1, ViewBag.temp as SelectList, new { @class = "form-control" })
就是改了下viewbag的命名,使得ID名与viewbag名不一样,就能够正常使用默认值了,我觉得这算是微软的一个bug了吧。