Home The ViewData item that has the key 'Catagory_id' is of type 'System.Int32' but must be of type 'IEnumerable<SelectListItem>'
Reply: 1

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

faseeh
1#
faseeh Published in 2018-02-13 18:14:52Z

This question already has an answer here:

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

this is the table that have a relation with Category_id as you can see

and im using entity framework so you can see the class

this is my edit (get)controller

when i run my application it gives me this error on dropdownlist

i want to retrieve category_name data from the catalog_categories to show which product belongs to which category. need help

Shyju
2#
Shyju Reply to 2018-02-13 18:45:06Z

When you use the DropDownList helper to render a SELECT element like this

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

The helper method looks for an item called Category_Id in the ViewBag dictionary and if it is there and it has a list of SelectListItem's, the helper will be able to render the SELECT element options from that collection.

So let's say if action method has code like below before calling the return View() statement

var list = new List<SelectListItem>
{
    new SelectListItem {Value = "1", Text = "Category 1"},
    new SelectListItem {Value = "2", Text = "Category 2"},
    new SelectListItem {Value = "3", Text = "Category 3"},
};
ViewBag.Category_Id = list;

The SELECT element will be rendered using the 3 SelectListItem objects we passed to ViewBag.Category_Id.

Now in your case, it is the edit screen and you do not have a view model, what you have to do is explcitily specify a different collection in your DropDownList helper method overload.

Like this

@Html.DropDownList("Category_Id",ViewBag.CategoryList as List<SelectListItem>
                                                       ,new { @class="form-control"})

For this code to work, we need to make sure that we are setting a list in the action method to ViewBag.CategoryList

var list = new List<SelectListItem>
{
    new SelectListItem {Value = "1", Text = "Category 1"},
    new SelectListItem {Value = "2", Text = "Category 2", Selected = true},
    new SelectListItem {Value = "3", Text = "Category 3"},
};
ViewBag.CategoryList= list;

You can replace the hard coded SelectListItem with the data from your table. Something like this (update your class/property names as needed)

var categoryId=cat_product.Catagory_id;
var list = db.CatalogCategories
                  .Select(a=>new SelectListItem { 
                                            Value = a.Id.ToString(),
                                            Text=a.Category_Name, 
                                            Selected = ( categoryId!=null && 
                                                                categoryId.Value==a.Id)
                         })
            .ToList();

While this might fix the issue, You have another (more clean and readable and clean ) option of using a view model along with the Html.DropDownListFor helper method. I strongly recommend that approach. You can find of tons of sample on SO for that approach.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.326464 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO