Mvc 응용 프로그램에서 사용자 정의 모델 바인더를 만들고 문자열을 열거 형 값으로 구문 분석하고이를 모델 속성에 할당하려고합니다. BindProperty
메서드를 재정의하는 방법을 찾았지만 SetProperty
메서드가있는 것으로 나타났습니다.IModelBinder의 BindProperty와 SetProperty의 차이점
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor)
{
switch (propertyDescriptor.Name)
{
case "EnumProperty":
BindEnumProperty(controllerContext, bindingContext);
break;
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
private static void BindEnumProperty(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var formValue = controllerContext.HttpContext.Request.Form["formValue"];
if (String.IsNullOrEmpty(formValue))
{
throw new ArgumentException();
}
var model = (MyModel)bindingContext.Model;
model.EnumProperty = (EnumType)Enum.Parse(typeof(EnumType), formValue);
}
나는 둘 사이의 차이점과 내가 권장하는 방식으로이 작업을 수행하는지 여부를 잘 모릅니다.
소스에서는 무효, http://aspnetwebstack.codeplex.com/SourceControl/latest#src/System.Web.Mvc/DefaultModelBinder.cs – Rob
@ 롭 귀하의 URL도 –
@Kilanny 무효에 지금? 그것은 여전히 나를 위해 일하는 것 같습니다. – Rob