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 w开发者_JS百科orking, 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.
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?
精彩评论