Has the asp.net mvc team implemented a default model binding for enums? One that is out of the box and there is no need of creating a custom model binder for enums.
It was there even with earlier versions. This html and Gender = Male
form value is correctly binding to Gender
enum property.
<select id="Gender" name="Gender">
<option value="Male">Male</option>
<option value="Female">Femal</option>
</select>
For the server side I find it easiest to use select lists in my view model
public class User
{
public UserType UserType { get; set; }
public IEnumerable<SelectListItem> UserTypesSelectList { get; set; }
public User()
{
UserTypesSelectList = Enum.GetNames(typeof(UserType)).Select(name => new SelectListItem()
{
Text = name,
Value = MakeEnumMoreUserFriendly(name)
});
}
}
public enum UserType
{
First,
Second
}
And in view
@Html.DropDownListFor(model => model.UserType, Model.UserTypesSelectList)