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的糟糕使用,并通过使用一个视图模型) –