关于mvc中@Html.DropDownListFor和@Html.DropDownList默认值无法选中问题简单总结
当我们在做类似编辑功能的时候,会给定select选中默认值,然而mvc中偶尔这个功能不能用,或者是强类型的@Html.DropDownListFor不能用。凑巧今天遇到问题,解决问题时发现了mvc的一个小bug,做记录如下:
后台无论用viewbag或是viewdata都不是重点,前台无论用@Html.DropDownList @Html.DropDownListFor都无所谓
重点是当后台用ViewData["typeBtnList"]=..... 或者ViewBag.typeBtnList =......前台用
@Html.DropDownList("typeBtnList", ViewData["typeBtnList"] as List<SelectListItem>, new { @class = "form-control" })或者是
@Html.DropDownList("typeBtnList", ViewBag.typeBtnList as List<SelectListItem>, new {@class = "form-control"})都不能如愿选中默认值
而当我们把前台改成
@Html.DropDownList("aaaaa", ViewData["typeBtnList"] as List<SelectListItem>, new { @class = "form-control" })或者
@Html.DropDownList("aaaaa", ViewBag.typeBtnList as List<SelectListItem>, new {@class = "form-control"})便可以正常选中
因为看不到源码,故猜测问题便是名称问题