[英]Custom model binding on derived property not working
我有一个自定义的ModelBinder(MVC3),由于某种原因而没有被解雇。 以下是相关的代码段:
视图
@model WebApp.Models.InfoModel
@using Html.BeginForm()
{
@Html.EditorFor(m => m.Truck)
}
EditorTemplate
@model WebApp.Models.TruckModel
@Html.EditorFor(m => m.CabSize)
ModelBinder的
public class TruckModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
throw new NotImplementedException();
}
}
Global.asax中
protected void Application_Start()
{
...
ModelBinders.Binders.Add(typeof(TruckModel), new TruckModelBinder());
...
}
InfoModel
public class InfoModel
{
public VehicleModel Vehicle { get; set; }
}
VehicleModel
public class VehicleModel
{
public string Color { get; set; }
public int NumberOfWheels { get; set; }
}
TruckModel
public class TruckModel : VehicleModel
{
public int CabSize { get; set; }
}
调节器
public ActionResult Index(InfoModel model)
{
// model.Vehicle is *not* of type TruckModel!
}
为什么我的自定义ModelBinder不触发?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.