I have a view model that contains enum with nullable type like this one :
public StudyLevel? studyLevel { get; set; }
I have made custom html helper to display a dropdownlist for rendering the enum into the view, the nullable case is displayed using
<option value="null">No value</option>
the problem is that when i submit the form modelstate give me the error :
studylevel cannot be "null" .
Could you suggest me any way to help me handle the nullable type in the view ?
What if you change your html helper to display the null value like this?
Or like this?
Edit: A better option (IMO) is to create a collection of
SelectListItemand then use a built-in HTML helper. Please take a look into this article I created some months ago, it may help you. You can use it like this:More info here:
ASP.NET MVC: Creating localized DropDownLists for enums