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

None

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?

Ehsan Sajjad

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

From Dev

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

From Dev

Why is there an error, "There is no ViewData item of type 'IEnumerable<SelectListItem>'..."

From Dev

There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Country.Name'. even when I don't use ViewData

From Dev

The ViewData item that has the key 'JobTitleID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'?

From Dev

Exception "There is no ViewData item of type 'IEnumerable<SelectListItem>" MVC

From Dev

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

From Dev

c# MVC Dropdownlist - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key ' '

From Dev

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

From Dev

The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

From Dev

ASP.NET MVC - Dropdown - There is no ViewData item of type 'IEnumerable<SelectListItem>'

From Dev

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

From Dev

The key 'CategoryID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

From Dev

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

From Dev

DropDownListFor no ViewData item of type IEnumerable

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

The ViewData item that has the key 'CountryId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

From Dev

ASP.NET MVC - Dropdown - There is no ViewData item of type 'IEnumerable<SelectListItem>'

From Dev

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

From Dev

"There is no ViewData item of type 'IEnumerable<SelectListItem>'" error with custom model and list

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

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

From Dev

Scaffolded model/view: The ViewData item that has the key 'COLUMN' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'

Related Related

  1. 1

    The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

  2. 2

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

  3. 3

    Why is there an error, "There is no ViewData item of type 'IEnumerable<SelectListItem>'..."

  4. 4

    There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'Country.Name'. even when I don't use ViewData

  5. 5

    The ViewData item that has the key 'JobTitleID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'?

  6. 6

    Exception "There is no ViewData item of type 'IEnumerable<SelectListItem>" MVC

  7. 7

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

  8. 8

    c# MVC Dropdownlist - There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key ' '

  9. 9

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

  10. 10

    The ViewData item that has the key 'CategoryId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

  11. 11

    ASP.NET MVC - Dropdown - There is no ViewData item of type 'IEnumerable<SelectListItem>'

  12. 12

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

  13. 13

    The key 'CategoryID' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

  14. 14

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

  15. 15

    DropDownListFor no ViewData item of type IEnumerable

  16. 16

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

  17. 17

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

  18. 18

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

  19. 19

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

  20. 20

    The ViewData item that has the key 'CountryId' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'

  21. 21

    ASP.NET MVC - Dropdown - There is no ViewData item of type 'IEnumerable<SelectListItem>'

  22. 22

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

  23. 23

    "There is no ViewData item of type 'IEnumerable<SelectListItem>'" error with custom model and list

  24. 24

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

  25. 25

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

  26. 26

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

  27. 27

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

  28. 28

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

  29. 29

    Scaffolded model/view: The ViewData item that has the key 'COLUMN' is of type 'System.String' but must be of type 'IEnumerable<SelectListItem>'

HotTag

Archive