0

I am using MVC Core with repository Pattern to handle operations. I am trying to generate a select element using DropDownListFor but recieve the following error:

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

The main model(Products) with be using data as IEnumerable<SelectListItem> from other models. Here is the code in the (Products) View:

@HTML.DropDownListFor(c => c.Products.CategoryId, Models.CategoryList, "--Select Category--", new { @class = "form-control" })

https://github.com/AliZ801/ShoppingCenter

pinkfloydx33
  • 11,863
  • 3
  • 46
  • 63
  • this can help you https://stackoverflow.com/a/3057931/6527049 – Vivek Nuna Jun 13 '20 at 11:01
  • I am using dynamic list data instead of static – Ali Zubair Jun 13 '20 at 12:30
  • Does this answer your question? [The ViewData item that has the key 'XXX' is of type 'System.Int32' but must be of type 'IEnumerable'](https://stackoverflow.com/questions/34366305/the-viewdata-item-that-has-the-key-xxx-is-of-type-system-int32-but-must-be-o) – Farhad Zamani Jun 13 '20 at 12:58
  • I have something similar. In repository, I created a method special for drop down list and assigned the value to it and then simply just called that method in controller. Even though i tried this 1 but same error! – Ali Zubair Jun 13 '20 at 17:07
  • public IEnumerable GetCategoryListForDropDown() { return _db.Category.Select(i => new SelectListItem() { Text = i.CatName, Value = i.Id.ToString() }); } //This method is called in constructor for Category List – Ali Zubair Jun 13 '20 at 17:26

0 Answers0