Trafalgar D Law Trafalgar D Law - 1 year ago 79
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?.

Answer Source

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;" })
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download