MVC 3 사용자 정의 ModelBinder가 사용되지 않습니다
-
12-11-2019 - |
문제
나는 숨겨진 변수의 무리를 같은 이름으로 게시하는 양식을 가지고 있습니다.게시물은 다음과 같습니다.
OfferId=3802&DeliveryDate=11%2F02%2F2011&DeliveryTime=12%3A00&location=1&location=698
.
MVC 작업에 게시합니다.
[HttpPost]
public ActionResult DeliveryOptions(DeliveryOptions model)
{
...
}
.
velingionOptions 모델은 다음과 같습니다.
public class DeliveryOptions
{
public long? OfferId { get; set; }
[CustomValidation(typeof(CustomValidator), "IsDeliveryDateValid")]
public DateTime? DeliveryDate { get; set; }
public DateTime? DeliveryTime { get; set; }
[CustomValidation(typeof(CustomValidator), "IsLocationsValid")]
public OfferLocations Locations { get; set; }
}
.
이제, 게시 된 location
변수를 offerlocations 객체에 구문 분석하고 싶다. 이것은 다음과 같습니다.
[ModelBinder(typeof(OfferLocationsModelBinder))]
public class OfferLocations
{
[Required]
public int[] LocationIds { get; set; }
}
.
모델 바인더는 현재 다음과 같습니다.
public class OfferLocationsModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
throw new NotImplementedException();
}
}
.
문제는 notimplementedException에서 깨질 수 없습니다.모델 바인더가 실행되지 않습니다.나는 아마도 분명히 뭔가를 놓쳤을 것입니다.어떤 아이디어도 있습니까?
해결책 3
이 이것을 일할 수 없었습니다.해결 방법을 구현해야했습니다.
이제 위치 ID를 CSV 문자열로 게시하고 사용자 정의 모델 바인더가 픽업 중이므로 배열에 파싱 할 수 있습니다.수치심 나는 다른 방법을 할 수 없었습니다.
다른 팁
System.Web.Mvc.DefaultModelBinder
에서 모델 바인더를 상속하고 object BindModel(ControllerContext, ModelBindingContext)
메소드를 무시하십시오.이 작업이 작동하는지 확인하십시오.그렇다면 - 거기에서 일할 수 있습니다 :)
이제 내가하고있는 일입니다.기본적으로, 나는 특정 유형이 요청에 있으면이 값에 대해 메소드가 실행되는지 확인해야했습니다.모델 바인더 등록 코드는 정확히 동일합니다 - 속성의 클래스에 적용된 속성입니다.
모델 바인더는 다음과 같습니다 :
public class TableModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var result = base.BindModel(controllerContext, bindingContext) as ITableModel;
if (result != null)
result.UpdateSorter();
return result;
}
}
.
p.s.기본 모델 바인더에서 파생되면 표준 MVC 코드를 사용하여 모든 속성을 바인딩하는 추가 이점을 제공하고 deserialized 객체를 확장 할 수 있습니다.
희망이 도움이
는 DepedencyResolver에 등록 된 ModelBinder입니까?MVC가 ModelBinder를 찾는 데 어려움을 겪고 있으며 기본값으로 뒤로 떨어지는 경우 궁금합니다.