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

Solution 1:

The mistake is you are putting it in ViewBag.ProcedureSubCategoryID while you are passing ProcedureID int Html.DropDownList() and also you are passing SelectList parameter null. A quick fix is to just replace ProcedureSubCategoryID with ProcedureID in Html.DropDownList() as key in first parameter:

you have three ways to resolve this.

Way 1:

Instead of passing null in second parameter which accepts of type SelectListyou can do something like this:

@Html.DropDownList("ProcedureID", ViewBag.ProcedureSubCategoryID as SelectList, new  { @class="form-data" })

Way2:

public ActionResult Create()
{
  ViewBag.DepartmentID = new SelectList(db.Departments, "ID", "Name");
  ViewBag.ProcedureSubCategoryID = new SelectList(db.ProcedureSubCategories, "ID", "Name");
  return View();
}


@Html.DropDownList("ProcedureSubCategoryID", null, new { @class = "form-control" })

Way 3:

or alternative is to store it in ProcedureID in your action:

public ActionResult Create()
{
       ViewBag.DepartmentID = new SelectList(db.Departments, "ID", "Name");
       ViewBag.ProcedureID = new SelectList(db.ProcedureSubCategories, "ID", "Name");
       return View();
}

and in View:

@Html.DropDownList("ProcedureID", null, new { @class = "form-control" })

Solution 2:

I suppose you're missing something like

ViewBag.ProcedureID = new SelectList(db.ProcedureCategory, "ID", "Name");

I think you have to inizialize ViewBag.ProcedureId