控制者

DepartmentLevel departmentlevel = new DepartmentLevel();
ViewBag.DepartmentLevel =
    new SelectList(db.DepartmentLevels, "DepartmentLevelId", "DepartmentLevelDesc",
    departmentlevel.DepartmentLevelId).OrderBy(a => a.Text);


的HTML

@Html.DropDownList("DepartmentLevel", ViewData["DepartmentLevel"] as SelectList,
                   "All Department", new { @style = "width:200px;" })


问题是什么?。

最佳答案

您在ViewBag中使用了ViewData而不是Controller。因此,您也应该在ViewBag中使用View,如下所示:

@Html.DropDownList("DepartmentLevel", ViewBag.DepartmentLevel as SelectList,
                   "All Department", new { @style = "width:200px;" })

09-03 17:47