- ASP.NET MVC DropDownListFor或DropDownList 不能設置默認選項
關鍵點在於ViewBag.XX不能與字段名同名。否則無法設置默認值。應該是因爲衝突。
例如:
@Html.DropDownListFor(model => model.dept_id, ViewBag.depts as IEnumerable<SelectListItem>,string.Empty, new { style = "width:180px" })
不能寫成:
@Html.DropDownListFor(model => model.dept_id,ViewBag.dept_id as IEnumerable<SelectListItem>,string.Empty, new { style = "width:180px" })