Trafalgar D Law
Trafalgar D Law

Reputation: 177

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?.

Upvotes: 2

Views: 935

Answers (1)

Salah Akbari
Salah Akbari

Reputation: 39976

You have used ViewBag instead of ViewData in your Controller. So you should use ViewBag in your View too, like this:

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

Upvotes: 4

Related Questions