2

i am getting error after post, when i am using dropdown in my mvc website, the code is as follows

ViewData["JobSite_JobCategories1"] = new SelectList(context.JobSite_JobCategories, "Id", "Category", null);

<%= Html.DropDownList("JobCategory", ((IEnumerable<SelectListItem>)ViewData["JobSite_JobCategories1"]))%>
<%= Html.ValidationMessageFor(model => model.JobCategory) %>

The validation is not working, and also after i catch error i fill the viewdata["JobSite_JobCategories1"] with selectlist again but still, it gives error

There is no ViewData item of type 'IEnumerable' that has the key 'JobCategory'.

Please can any one suggest me the solution to this problem, any articles, samples, links.

Kris van der Mast
  • 16,343
  • 8
  • 39
  • 61
Milan Solanki
  • 1,207
  • 4
  • 25
  • 48
  • please provide us the code in your action and explain which post you are referring to (initial load or submit of the view?) – Tahbaza Aug 01 '10 at 12:48
  • http://stackoverflow.com/questions/1012924/there-is-no-viewdata-item-with-the-key-tasktypes-of-type-ienumerableselectlist – Alex Aug 01 '10 at 12:51
  • and in general, you might find your answer somewhere here: http://stackoverflow.com/search?q=There+is+no+ViewData+item+of+type – Alex Aug 01 '10 at 12:52

1 Answers1

2

Everything looks correct mostly.

You are building the SelectList properly here:

ViewData["JobSite_JobCategories1"] = new SelectList(context.JobSite_JobCategories, "Id", "Category", null); 

I am not sure why you are not just casting it to what it is here, a SelectList:

<%= Html.DropDownList("JobCategory", (SelectList)(ViewData["JobSite_JobCategories1"]))%>

Any reason you are not using DropDownListFor to build it? Eg:

<%= Html.DropDownList(m => m.JobCategory, (SelectList)(ViewData["JobSite_JobCategories1"]))%>

Also I posted this question a while back that might be of interest for you to read:

Best way of implementing DropDownList in ASP.NET MVC 2?

Community
  • 1
  • 1
Kelsey
  • 47,246
  • 16
  • 124
  • 162