Trafalgar D Law Trafalgar D Law - 1 month ago 13
C# Question

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

Controller

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;" })


What is the problem?.

Answer

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;" })
Comments