當咱們在作相似編輯功能的時候,會給定select選中默認值,然而mvc中偶爾這個功能不能用,或者是強類型的@Html.DropDownListFor不能用。湊巧今天遇到問題,解決問題時發現了mvc的一個小bug,作記錄以下:mvc
後臺不管用viewbag或是viewdata都不是重點,前臺不管用@Html.DropDownList @Html.DropDownListFor都無所謂spa
重點是當後臺用ViewData["typeBtnList"]=..... 或者ViewBag.typeBtnList =......前臺用orm
@Html.DropDownList("typeBtnList", ViewData["typeBtnList"] as List<SelectListItem>, new { @class = "form-control" })或者是源碼
@Html.DropDownList("typeBtnList", ViewBag.typeBtnList as List<SelectListItem>, new {@class = "form-control"})都不能如願選中默認值form
而當咱們把前臺改爲class
@Html.DropDownList("aaaaa", ViewData["typeBtnList"] as List<SelectListItem>, new { @class = "form-control" })或者後臺
@Html.DropDownList("aaaaa", ViewBag.typeBtnList as List<SelectListItem>, new {@class = "form-control"})即可以正常選中List
由於看不到源碼,故猜想問題即是名稱問題select