There is no ViewData item of type 'IEnumerable

2019-01-12 10:31发布

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?

3条回答
甜甜的少女心
2楼-- · 2019-01-12 11:03

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();
    }
查看更多
再贱就再见
3楼-- · 2019-01-12 11:13

I suppose you're missing something like

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

I think you have to inizialize ViewBag.ProcedureId

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-12 11:17

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" })
查看更多
登录 后发表回答