MVC 模型绑定
2012-02-14 09:52
176 查看
Model binding is the process of creating .NET objects using the data sent by the browser in an Http request.
英文资料给出的定义:把Http请求传来的数据,封装成对象的过程。
2.原理:根据参数的名字,从数据源中寻找相同的键值对,然后赋值
3.支持类嵌套,支持数组,只要参数的类型和model的类型一致,就可以完全映射
4.可以屏蔽字段:Bind(Include="字段1,字段2"),Bind(Exclude="字段1,字段2")
5.可以手动触发:UpdateModel(),TryUpdateModel()
英文资料给出的定义:把Http请求传来的数据,封装成对象的过程。
默认模型绑定器:
1.按顺序从四个数据源中寻找数据:Request.Form,RouteData.Values,Request.QuerySting,Request.Files2.原理:根据参数的名字,从数据源中寻找相同的键值对,然后赋值
3.支持类嵌套,支持数组,只要参数的类型和model的类型一致,就可以完全映射
4.可以屏蔽字段:Bind(Include="字段1,字段2"),Bind(Exclude="字段1,字段2")
5.可以手动触发:UpdateModel(),TryUpdateModel()
自定义模型绑定:
顾名思义,定义了 封装对象的过程,这个过程交给一个实现了IModelBinder接口的类;// 例子: public class UserContextBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { return controllerContext.HttpContext.Session["UserContext"] as UserContext; } } // 在Global.asax中注册 protected void Application_Start() { // 注册模型绑定器 ModelBinders.Binders.Add(typeof(UserContext), new UserContextBinder()); }
相关文章推荐
- ASP.NET Core MVC/WebAPi 模型绑定
- ASP.NET MVC数组模型绑定
- MVC 手写模型绑定
- MVC复杂类型的模型绑定
- Asp.net MVC 3 Framework 第十七章 模型绑定
- ASP.NET MVC 4 (九) 模型绑定
- MVC重写DefaultModelBinder实现自定义模型绑定
- ASP.NET MVC Model Binding(模型绑定)
- MVC 框架-Model Binding(模型绑定)批量修改增加记录
- 详解ASP.NET MVC 常用扩展点:过滤器、模型绑定
- MVC模型绑定
- ASP.NET MVC——模型绑定
- The DefaultModelBinder in ASP.NET MVC -ASP.NET MVC的默认模型绑定器
- ASP.NET MVC中的模型绑定
- 如何在FineUIMvc(ASP.NET MVC)视图中绑定多个模型?
- ASP.NET MVC模型绑定
- ASP.NET MVC模型绑定的6个建议
- MVC 模型绑定
- MVC模型绑定-input Name与model Name
- ASP.NET MVC学习之模型绑定(2)