There is no ViewData item of type 'IEnumerable' that has the key “key”

前端 未结 2 886
别跟我提以往
别跟我提以往 2020-12-02 02:08

My code is like this

 namespace DiagnosisApp.Models
{
    public class ProcedurePrice
    {
        public int ID { get; set; }


        public int Departme         


        
相关标签:
2条回答
  • 2020-12-02 03:06

    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" })
    
    0 讨论(0)
  • 2020-12-02 03:08

    I suppose you're missing something like

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

    I think you have to inizialize ViewBag.ProcedureId

    0 讨论(0)
提交回复
热议问题