關於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"})便可以正常選中

因為看不到源碼,故猜測問題便是名稱問題

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM