new*_*bie 14 c# model-binding asp.net-mvc-4
我在global.asax中为MyList注册了一个自定义模型绑定器.但是,模型绑定器不会触发嵌套属性,对于简单类型,它可以正常工作.在下面的示例中,它会触发Index()但不会触发Index2()
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);
}
Run Code Online (Sandbox Code Playgroud)
码:
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();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
4529 次 |
最近记录: |