How to bind asp.net mvc 4 model for enumerations?

Has the asp.net mvc command implemented a default model binding for enumerations? The one that is out of the box and there is no need to create a custom binder for transfers.

UPDATE:
Let's say I have an action that will receive the view model, and the JSON object will be sent to the action.

jsObj{id:2, name:'mike', personType: 1} 

and presentation model:

 class ViewModel { public int id {get;set;} public string name {get;set;} public PersonType personType{get;set;} } public enum PersonType : int { Good = 1, Bad = 2, Evil = 3 } 

Will the type of person be attached?

+8
asp.net-mvc-4 model-binding
source share
1 answer

He was there even with earlier versions. This html and Gender = Male form value is correctly bound to the 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 sight

 @Html.DropDownListFor(model => model.UserType, Model.UserTypesSelectList) 
+4
source share

All Articles