Click here to Skip to main content
15,888,301 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
There is no ViewData item of type 'IEnumerable<SelectListItem>' that has the key 'categoryId'.


This is my code
Controller:
[HttpGet]
C#
public ActionResult AddNewProduct()
       {

C#
ViewBag.Category = new List<SelectListItem>
                               {

                                   new SelectListItem{Text="DELL",Value="1"},
                                   new SelectListItem{Text="HP",Value="2"},
                                   new SelectListItem{Text="APPLE",Value="3"},
                                   new SelectListItem{Text="ACER",Value="4"},
                                   new SelectListItem{Text="LENOVO",Value="5"},
                                   new SelectListItem{Text="SAMSUNG",Value="6"},
                                   new SelectListItem{Text="ASUS",Value="7"}
                               };


           return View("AddNewProduct");
       }


//Razor View


@Html.DropDownListFor(
m => m.categoryId,
@ViewBag.Category as List<SelectListItem>,
new { @class="dropdownList", style = "width:25%;" })


What I have tried:

http://www.asp.net/mvc/overview/older-versions/working-with-the-dropdownlist-box-and-jquery/using-the-dropdownlist-helper-with-aspnet-mvc
Posted

1 solution

The model you passed into the Razor view from your controller doesn't have a property called "categoryId". How much more clearer does the error message have to be?

We can't say anything else about it because you haven't shown the appropriate code. You show nothing from the controller returning this view and nothing of the Razor view that should be getting a view model object from the controller.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900