" that has the key "key" My code is like this namespace DiagnosisApp.Models {...">

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

My code is like this

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


        public int DepartmentID { get; set; }
        public int ProcedureID { get; set; }
        public int InsuranceProviderID { get; set; }
        public int ProcedureCategoryID { get; set; }
        public int ProcedureSubCategoryID { get; set; }



        [ForeignKey("ID")]
        public virtual Department Department { get; set; }

        [ForeignKey("ID")]
        public virtual InsuranceProvider InsuranceProvider { get; set; }

        [ForeignKey("ID")]
        public virtual ProcedureCategory ProcedureCategory { get; set; }
        [ForeignKey("ID")]
        public virtual ProcedureSubCategory ProcedureSubCategory { get; set; }
    }
}

      

controller

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

      

And in my opinion

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

      

Everything looks good to me, but it throws an error

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

      

Can anyone point out what I am doing wrong here?

+3


source to share


3 answers


The error is that you are putting it in ViewBag.ProcedureSubCategoryID

while you are passing an ProcedureID

int Html.DropDownList()

and also passing a SelectList

parameter null

. A quick fix is ​​to simply replace ProcedureSubCategoryID

with ProcedureID

in Html.DropDownList()

as the key in the first parameter:

you have three ways to solve this problem.

Method 1:

Instead of passing null

in a second parameter that takes a type SelectList

, you 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" })

      

Method 3:

or an 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 the view:

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

      

+7


source


I am assuming you are missing something like

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

      



I think you need to initialize ViewBag.ProcedureId

+1


source


You are using a partial file

if you are using partial, it is very simple, you may find a solution by mistake this error says you don't have View Data With type SelectListItem, that the key was "ProcedureId", I think you are using Partial for your creation and call it on your index page So you have to pass this code on the result of the index action just like my code:

public ActionResult Index()
    {
        ViewData["ProcedureID"] = new SelectList(db.Procedures, "ProcedureId", "ProcedureName");
        return View();
    }


    public ActionResult Create()
    {
        return View();
    }

      

0


source







All Articles