Asp.net Mvc模型绑定
Asp.net Mvc模型绑定
Asp.net Mvc模型绑定一、Asp.net Mvc模型绑定的认识
任何控制器方法的执行都受action invoker组件(下文用invoker代替)控制。对于每个Action方法的参数,这个invoker组件都会获取一个Model Binder Object(模型绑定器对象)。Model Binder的职责包括为Action方法参数寻找一个可能的值(从HTTP请求上下文)。每个参数都可以绑定到不同的Model Binder;但是大部分情况我们都使用的是默认模型绑定器-DefaultModelBinder(如果我们没有显式设置使用自定义的Model Binder的话)。
每个Model Binder都使用它自己的特定算法来为Action方法参数设置值。默认模型绑定器对象大量使用反射机制。具体来说,对于每个Action方法参数,Model Binder都试图根据参数名去请求参数中寻找匹配的值。比如某个Action方法参数名为Text,那么ModelBinder会去请求上下文中寻找拥有相同名字的名值对(Entry)。如果找到,则Model Binder继续将Entry的值转换为Action方法参数类型。如果类型转换成功,转换后的值就被赋给那个Action方法参数,否则会抛出异常。
二、Asp.net Mvc默认模型绑定器特性
1.按顺序从四个数据源中寻找数据:Request.Form,RouteData.Values,Request.QuerySting,Request.Files
请求数据来源 | 说明 |
Request.Form | 通过表单提交的参数 |
RouteData.Values | 路由参数 |
Request.QueryString | 查询参数 |
Request.Files | 随请求上传的文件 |
2.原理:根据参数的名字,从数据源中寻找相同的键值对,然后赋值
3.支持类嵌套,支持数组,只要参数的类型和model的类型一致,就可以完全映射
4.可以屏蔽字段:Bind(Include="字段1,字段2"),Bind(Exclude="字段1,字段2")
5.可以手动触发:UpdateModel(),TryUpdateModel()
三、Asp.net Mvc模型绑定实例
1、没有模型绑定的时候
public ActionResult Example0()
{
if (Request.Form.Count > 0)
{
string id = Request.Form["Id"];
string fname =Request.Form["FirstName"];
string lname = Request.Form["LastName"];
ViewBag.StatusMessage = "Employee data received successfully for ID " + id + "!";
}
return View();
}
2、简单绑定数据
[HttpPost]
public ActionResult Example1(string id, string firstname, string lastname)
{
ViewBag.StatusMessage = "Employee data received successfully for ID " + id + "!";
return View();
}
页面内容
<tr>
...
<td>
<input name="Id" type="text" />
</td>
</tr>
<tr>
...
<td>
<input name="FirstName" type="text" />
</td>
</tr>
<tr>
...
<td>
<input name="LastName" type="text" />
</td>
</tr>
3、绑定一个类类型
[HttpPost]
public ActionResult Example2(Employee emp)
{
ViewBag.StatusMessage = "Employee data received successfully for ID " + emp.Id + "!";
return View();
}
类如下:
public class Employee
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}
4、绑定一个类的属性
[HttpPost]
public ActionResult Example3(Employee emp)
{
ViewBag.StatusMessage = "Employee data received successfully for ID " + emp.Id + "!";
return View();
}
类如下:
public class Employee
{
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public Address HomeAddress { get; set; }
}
public class Address
{
public string Street { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
}
页面内容:
<tr>
...
<td>
<input name="HomeAddress.Street" type="text" /></td>
</tr>
...
<td>
<input name="HomeAddress.Country" type="text" /></td>
</tr>
...
<td>
<input name="HomeAddress.PostalCode" type="text" /></td>
</tr>
5、绑定简单类型的集合
[HttpPost]
public ActionResult Example4(IList<string> id, IList<string> name)
{
ViewBag.StatusMessage = "Employee data received successfully for " + id.Count + " records!";
return View();
}
页面内容:
...
<tr>
<td align="right" nowrap="nowrap" width="15%">
<input name="id" type="text" size="20" /></td>
<td>
<input name="name" type="text" />
</td>
</tr>
<tr>
<td align="right" nowrap="nowrap" width="15%">
<input name="id" type="text" size="20" />
</td>
<td>
<input name="name" type="text" />
</td>
</tr>
<tr>
<td align="right" nowrap="nowrap" width="15%">
<input name="id" type="text" />
</td>
<td>
<input name="name" type="text" />
</td>
</tr>
...
6、绑定一个类的集合
[HttpPost]
public ActionResult Example5(IList<Employee> employees)
{
ViewBag.StatusMessage = "Employee data received successfully for " + employees.Count + " records!";
return View();
}
页面内容:
...
<
标签:MVC
您可能感兴趣
- mysql mvcc 流程(Mysql MVCC机制原理详解)
- iiS7中如何部署MVC3程序
- MVC中JavaScriptResult
- MVC使用HandleErrorAttribute自定义异常
- mvc中@helper的用法
- Asp.net Mvc模型绑定
- MVC中BeginForm
- asp.net MVC处理流程
- MVC中使用DropDownList
- MVC 使用FileResult导出文件
- mvc中@RenderSection()
- MVC中报get_RazorKeywords()错误的解决方法
- mvc中validateinput属性失效
- mvc JavaScriptResult的用法
- php框架初始化教程学习(PHP从零开始打造自己的MVC框架之类的自动加载实现方法详解)
- mvc中使用uploadify批量上传
- ()
- ()
- 800壮士拼死拖住30万日军 八佰 的真实历史,誓与阵地共存亡(800壮士拼死拖住30万日军)
- 演员陈创,火于 哮天犬 ,颠峰于 福贵 ,现状却令人唏嘘(演员陈创火于哮天犬)
- 幼小衔接-20以内看图读数 写数 数的组成练习题(幼小衔接-20以内看图读数)
- 你只要花上20天记单词,英语成绩就能从57提到100(你只要花上20天记单词)
热门推荐
- 宝塔面板怎么设置ftp(新安装宝塔Linux面板FTP无法连接的解决方法)
- 怎么删掉laravel(laravel-admin的图片删除实例)
- extjs中treegrid
- formdata上传图片参数(data:image data url 文件转为Blob上传后端的方法)
- 虚拟环境python详细教程(python创造虚拟环境方法总结)
- sqlserver2000安装之后在哪打开(SQL2000安装后,SQL Server组无项目解决方法)
- vue树形表格内容太长(VUE 无限层级树形数据结构显示的实现)
- nginx 配置解析(Nginx的rewrite模块详解)
- nginx网站集群(Nginx实现高可用集群构建Keepalived+Haproxy+Nginx)
- vue过滤器使用思路(vue 过滤器和自定义指令的使用)