There is no ViewData item of type 'IEnumerable

2019-01-12 10:35发布

问题:

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 view

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

Everything looks okay 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?

回答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" })


回答2:

I suppose you're missing something like

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

I think you have to inizialize ViewBag.ProcedureId



回答3:

are you use partial??

if you use partial, thats very simple ,you can find solution on error this error say you have not View Data With type SelectListItem that key was 'ProcedureId' i think you use Partial for your create and call it on your Index Page So you should passing this code on your index action result as like as my code:

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


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