I have
开发者_如何学编程[DisplayName("Country")]
public List<SelectListItem> Countries { get; set; }
property in strong-type Model View class for DropDownList.
When I try to check if the ModelState.IsValid on form postback it's always false & error for Countries tells "Can't convert [value] to SelectListItem" or some of a kind.
I figured out there is no straight-forward mapping for drop down selected value (looks like I'll have to read value from Form value collection), but how can I ignore binding and validation for List property? I just want to make ModelState.IsValid attribute to be true if all the other fields are populated properly.
Thank you in advance
Finally I used workaround.
My model now:
class Model
{
...
[DisplayName("Country")]
List<Country> Countries;
Guid CountrySelected <-- new field!
...
}
I use Html.DropDownList("CountrySelected", Model.Countries.Select(x => new SelectItemList.. )
instead of HtmlDropDownListFor. HtmlDropDownListFor maps [id selected] to List not to CountrySelected property. Now [id selected] is mapped to CountrySelected
Is it because the value submitted is of type String
or Country
rather than a list of SelectListItem
or List<SelectListItem>
.
What are you binding to the control in the UI?
try
[DisplayName("Country")]
public List<Country> Countries { get; set; }
Where Country
is the type name from your DAL.
EDIT:
Based on the error you are recieving, it sounds like the model is expecting the value to be a String
so try swapping List<Country>
for List<String>
.
[DisplayName("Country")]
public List<string> Countries { get; set; }
精彩评论