Html.DropDownList Validation Error With No Validation Set
I currently have no validation for my drop down list, but nevertheless, I get a validation error in my ModelState when no value is selected.
Is there any automated validation when you specify a default option?
It isn't part of the Model, so it def开发者_JS百科initely isn't a missed attribute!
<%= Html.DropDownList("CategoryId",
(IEnumerable<SelectListItem>)ViewData["categories"], "-- Select --")%>
I didn't get any suggestions on this one - but if you've arrived here looking for an answer, this is how I solved my issue:
Instead of passing in the default value to the DropDownList, I initialize the category list with a default instead (i.e. you put { "[YourDefaultValue]", "-- Select --" } as the first SelectListItem).
<%= Html.DropDownList("CategoryId",
(IEnumerable<SelectListItem>)ViewData["categories"])%>
精彩评论