예, 동일한 문제가 있습니다. 이상한 문제는 다시 보낸 경우 :
{"Sex" : 'Male'}
아무 문제가없는 것입니다. http://eliasbland.wordpress.com/2009/08/08/enumeration-model-binder-for-asp-net-mvc/
namespace yournamespace
{
/// <summary>
/// Generic Custom Model Binder used to properly interpret int representation of enum types from JSON deserialization, including default values
/// </summary>
/// <typeparam name="T">The enum type to apply this Custom Model Binder to</typeparam>
public class EnumBinder<T> : IModelBinder
{
private T DefaultValue { get; set; }
public EnumBinder(T defaultValue)
{
DefaultValue = defaultValue;
}
#region IModelBinder Members
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
return bindingContext.ValueProvider.GetValue(bindingContext.ModelName) == null ? DefaultValue : GetEnumValue(DefaultValue, bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue);
}
#endregion
public static T GetEnumValue<T>(T defaultValue, string value)
{
T enumType = defaultValue;
if ((!String.IsNullOrEmpty(value)) && (Contains(typeof(T), value)))
enumType = (T)Enum.Parse(typeof(T), value, true);
return enumType;
}
public static bool Contains(Type enumType, string value)
{
return Enum.GetNames(enumType).Contains(value, StringComparer.OrdinalIgnoreCase);
}
}
}
다음의 Global.asax에서 모델 바인더 등록 : 문제를 해결하기 위해, 나는 (일부 오류가 있었다으로 약간 수정) 여기에있는 예제를 활용, 열거 형에 대한 사용자 정의 모델 바인더를 구현했습니다. cs. 더 빠른 방법이 있는지
ModelBinders.Binders.Add(typeof(SexType), new EnumBinder<SexType>(SexType.Male));
나는 잘 모르겠지만,이 위대한 작품 : 는 귀하의 경우에 그것은 뭔가 같은 것입니다.