Trafalgar D Law Trafalgar D Law - 3 months ago 31
C# Question

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key '......'


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


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

What is the problem?.


You have used ViewBag instead of ViewData. So you should use ViewBag like this:

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