asp.net mvc中的模型绑定可以在提交http请求的时候,进行数据的映射。

1.没有模型绑定的时候

 public ActionResult Example0()
{
if (Request.Form.Count > )
{
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="" /></td>
<td>
<input name="name" type="text" />
</td>
</tr>
<tr>
<td align="right" nowrap="nowrap" width="15%">
<input name="id" type="text" size="" />
</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();
}

页面内容:

 ...
<tr>
<td align="right" nowrap="nowrap" width="15%">
<input name="[0].id" type="text" size="" />
</td>
<td>
<input name="[0].FirstName" type="text" />
</td>
<td>
<input name="[0].LastName" type="text" />
</td>
</tr>
<tr>
<td align="right" nowrap="nowrap" width="15%">
<input name="[1].id" type="text" size="" />
</td>
<td>
<input name="[1].FirstName" type="text" />
</td>
<td>
<input name="[1].LastName" type="text" />
</td>
</tr> ...

注意索引是从0开始,中间不间断

如果,遇到有动态的Add和Delete功能,则索引不好去设置,可以使用下面的方法:

添加一个隐藏控件,控件名称后缀为.Index

Controller不变,页面内容更改为:

 ...
<tr>
<td align="right" nowrap="nowrap" width="15%">
<input type="hidden" name="employees.Index" value="" />
<input name="employees[100].id" type="text" size="" />
</td>
<td>
<input name="employees[100].FirstName" type="text" />
</td>
<td>
<input name="employees[100].LastName" type="text" />
</td>
</tr>
<tr>
<td align="right" nowrap="nowrap" width="15%">
<input type="hidden" name="employees.Index" value="ccc" />
<input name="employees[ccc].id" type="text" size="" />
</td>
<td>
<input name="employees[ccc].FirstName" type="text" />
</td>
<td>
<input name="employees[ccc].LastName" type="text" />
</td>
</tr>
...

7.可自定义模型

 [HttpPost]
public ActionResult Example6([ModelBinder(typeof(EmployeeBinder1))]Employee employee)
{
ViewBag.StatusMessage = "Employee data received successfully for " + employee.Id + "!";
return View();
}

绑定方法:

 public class EmployeeBinder1:IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
Employee emp = new Employee();
emp.Id = "E" + controllerContext.HttpContext.Request.Form["Id"];
emp.FirstName = controllerContext.HttpContext.Request.Form["FirstName"];
emp.LastName = controllerContext.HttpContext.Request.Form["LastName"];
emp.BirthDate = new DateTime(int.Parse(controllerContext.HttpContext.Request.Form["year"]),
int.Parse(controllerContext.HttpContext.Request.Form["month"]),
int.Parse(controllerContext.HttpContext.Request.Form["day"]));
return emp;
}
}

Asp.net Mvc 中的模型绑定的更多相关文章

  1. ASP.NET MVC中的模型绑定

    模型绑定的本质   任何控制器方法的执行都受action invoker组件(下文用invoker代替)控制.对于每个Action方法的参数,这个invoker组件都会获取一个Model Binder ...

  2. ASP.NET MVC学习之模型绑定(1)

    一.前言 下面我们将开始学习模型绑定,通过下面的知识我们将能够理解ASP.NET MVC模型的模型绑定器是如何将http请求中的数据转换成模型的,其中我们重点讲述的是表单数据. 二.正文 1.简单类型 ...

  3. ASP.NET MVC中的模型装配 封装方法 非常好用

    下面说一下 我们知道在asp.net mvc中 视图可以绑定一个实体模型 然后我们三层架构中也有一个model模型 但是这两个很多时候却是不一样的对象来的 就拿微软的官方mvc例子来说明 微软的视图实 ...

  4. ASP.NET Core 中的模型绑定

    微软官方文档:ASP.NET Core 中的模型绑定 Route 是通过MVC Route URL取值. 如:http://localhost:5000/Home/Index/2,id取出的值就会是2 ...

  5. ASP.NET MVC学习之模型绑定(2)

    3.手工调用模型绑定 很多情况下我们都是通过形参的方式接收来自http流中的数据,这看似是完美的,但是缺少了很多过程中的控制,所以我们就需要使用手工的方式进行绑定.下面我们通过一个例子来说明,首先打开 ...

  6. [转]ASP.NET MVC 4 (九) 模型绑定

    本文转自:http://www.cnblogs.com/duanshuiliu/p/3706701.html 模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C ...

  7. ASP.NET MVC 4 (九) 模型绑定

    模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C#间起着桥梁的作用.模型绑定的一个最简单的例子是带参数的控制器action方法,比如我们注册这样的路径映射: ...

  8. ASP.NET MVC 下自定义模型绑定,去除字符串类型前后的空格

    直接贴代码了: SkyModelBinder.cs using System.ComponentModel; using System.Linq; using System.Web.Mvc; name ...

  9. ASP.NET MVC中默认Model Binder绑定Action参数为List、Dictionary等集合的实例

    在实际的ASP.NET mvc项目开发中,有时会遇到一个参数是一个List.Dictionary等集合类型的情况,默认的情况ASP.NET MVC框架是怎么为我们绑定ASP.NET MVC的Actio ...

随机推荐

  1. JS 中刷新页面的方法

    整理了就是这几种,,有些在IE下面是不支持的,慎用... 1,history.go(0) 2,location.reload() 3,location=location 4,location.assi ...

  2. Struts2 校验

    Struts2校验格式: actionName-methodName-invalidation.xml  该配置文件必须和action类在同一个包下. <?xml version="1 ...

  3. windows 数据类型转换为 dotnet 数据类型

    Windows Data Type .NET Data Type BOOL, BOOLEAN Boolean or Int32 BSTR String BYTE Byte CHAR Char DOUB ...

  4. PHP学习笔记,自己动手写个MVC的框架 -- base所有代码

    <?php /** 名称:基础类 作用:引用参数处理类,加载基础配置, 作者:swordphp@126.com 创建日期:2013-07-31 **/ require_once(ROOT_PAT ...

  5. 入门指引 - PHP手册笔记

    曾经简单的学习过PHP,看的是<PHP和MySQL Web开发>,还有万能的搜索引擎的帮助.这次准备系统的学习一下,参考资料是PHP Manual. PHP能做什么 PHP主要用于服务端的 ...

  6. 小巧数据库 Derby 使用攻略

    阅读目录 1. Derby 介绍 2. 稍稍配置下环境变量 3. Derby 操作和 Java 访问 回到顶部 1. Derby 介绍 将目光放在小 Derby 的原因是纯绿色.轻巧.内存占用小,分分 ...

  7. linux 下使用 tc 模拟网络延迟和丢包(转)

    1 模拟延迟传输简介 netem 与 tc: netem 是 Linux 2.6 及以上内核版本提供的一个网络模拟功能模块.该功能模块可以用来在性能良好的局域网中,模拟出复杂的互联网传输性能,诸如低带 ...

  8. C11 memory_order

    概念: 摘录自:http://preshing.com/20120913/acquire-and-release-semantics/ Acquire semantics is a property ...

  9. hdu1258Sum It Up (DFS)

    Description Given a specified total t and a list of n integers, find all distinct sums using numbers ...

  10. CentOS7 yum lamp 虚拟主机配置 lamp各组件简单影响性能的参数调整--for 一定的环境需求

    LAMP Server on CentOS 7 Updated Tuesday, January 13, 2015 by Joel Kruger This guide provides step-by ...