0
我是新的MVC,我想問你是否可以教我如何設置/顯示下拉列表中的默認項目?下拉Mvc顯示默認選擇的項目
在控制器..
[HttpGet]
public ActionResult Edit(int ID)
{
EmployeeDBEntities o = new EmployeeDBEntities();
Employee e = new Employee();
e = o.Employees.Single(x => x.EMP_ID == ID);
ViewBag.Dept = o.uspDeptCbo().Select(x => new SelectListItem { Value = x.DEPT_ID.ToString() , Text=x.DEPARTMENT });
return View(e);
}
查看
<div class="editor-field">
@Html.DropDownList("Dept",string.Empty)
@Html.ValidationMessageFor(model => model.DEPT_ID)
</div>
使用'@ Html.DropDownListFor(m => m.DEPT_ID,(IEnumerable)ViewBag.Dept,string.Empty)'(然後擺脫對ViewBag的糟糕使用,並通過使用一個視圖模型) –