[ASP.NET MVC] DropDownList Submit Null Value Problem in Search (Model Binding)
Published: (Updated: )
by .<tr>
<th style="text-align:right;">
@Html.LabelFor(model => model.BookClassName)
</th>
<th>
<div class="col-md-10">
@Html.DropDownList("DropDownList_BookClassName", (IEnumerable<SelectListItem>)ViewBag.CategoryItems, "-- select an option --", new { @class = "form-control form-select" })
</div>
</th>
</tr>
這是我在View 寫的 DropdownList Function
後來發現的正確的用法為
DropDownList(ModelField,[your data...],value,new class);
不知道有Model Binding的我瘋狂找尋是不是值沒傳到
後來才發現第一個欄位需與要傳的值在Model中所設的名字要一樣
以下為正確寫法
<tr>
<th style="text-align:right;">
@Html.LabelFor(model => model.BookClassName)
</th>
<th>
<div class="col-md-10">
@Html.DropDownList("BookClassName", (IEnumerable<SelectListItem>)ViewBag.CategoryItems, "-- select an option --", new { @class = "form-control form-select" })
</div>
</th>
</tr>