Tôi đã đăng ký một mô hình tùy chỉnh chất kết dính cho MyList trong global.asax. Tuy nhiên mô hình chất kết dính không cháy cho thuộc tính lồng nhau, cho các loại đơn giản nó hoạt động tốt. Trong ví dụ dưới đây, nó cháy cho Index() nhưng không phải không cháy cho Index2()Tùy chỉnh Mẫu Binder không cháy
Global.asax
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
ModelBinders.Binders.Add(typeof(MyList), new MyListBinder());
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Code:
public class MyListBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
return new MyList();
}
}
public class MyList
{
public List<int> items { get; set; }
}
public class MyListWrapper
{
public MyList listItems { get; set; }
}
public class TestController : Controller
{
public ActionResult Index(MyList list) // ModelBinder fires :-)
{
return View();
}
public ActionResult Index2(MyListWrapper wrapper) // ModelBinder does not fire! :-(
{
return View();
}
}
Bạn đã tìm thấy giải pháp cho vấn đề này chưa? –