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. 关于jquery对象和DOM对象的区别

    这个问题的出现是因为自己对jquery不够了解,只会获取简单的Demo,做简单的操作,将jquery的很多方法和js中的混淆,以为js中的很多方法,在jquery中也可以使用,这是完全错误的理解! 所 ...

  2. JS 中刷新页面的方法

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

  3. 生成简历经验总结(解析HTML字符串)

    在生成简历的过程中,我的做法是首先设计一个word的简历模板,设置好书签,从数据库中读取数据,调用aspose进行填充.一般的数据项包括图片文件都没有问题. 问题出在了HTML字符串上.因为简历中有几 ...

  4. 七牛云Fetch第三方资源并转码(PHP版)

    七牛云的图片加速一直在用,好用没得说,最近项目需要做个微信端录音,然后上传,别人试听的功能,录音和上传用的都是微信的接口,有文档,比较方便,但是上传后,微信只给保存3天,所以就下载到了七牛,也就用到了 ...

  5. VS2010中添加dll目录

    RT,比如用VS写QT,用qmake生成的项目,需要在项目属性里设置:调试->环境,path=%path%;C:\Qt\4.8.5\bin 这样省的每次都要把一堆dll复制到debug/rele ...

  6. 如何把Python2的代码转换为Python3的代码

    如何把Python2的代码转换为Python3的代码 注: 如果对于python2和python3不熟悉的,可以参考: [整理]总结Python2(Python 2.x版本)和Python3(Pyth ...

  7. js的数组操作

    用 js有很久了,但都没有深究过js的数组形式.偶尔用用也就是简单的string.split(char).这段时间做的一个项目,用到数组的地方很多,自以为js高手的自己居然无从下手,一下狠心,我学!呵 ...

  8. PF_RING packet overwrites

    最近在用 PF_RING 抓包过程中,发现个灵异的现象,高流量丢包时, 经常会出现正在处理的包的内容被覆盖.开始,怀疑是不是自己程序有地方越界写了,后来发现,如果自己拷贝一份,然后处理拷贝的那份,永远 ...

  9. linux之SQL语句简明教程---函数

    既然数据库中有许多资料都是已数字的型态存在,一个很重要的用途就是要能够对这些数字做一些运算,例如将它们总合起来,或是找出它们的平均值.SQL 有提供一些这一类的函数.它们是: AVG (平均) COU ...

  10. redhat enterprise 6.3 x86_64 上安装VirtualBox详细教程

    这个教程真难找..... 安装第一步遇到的问题就是不能使用yum安装包,这是由于redhat是收费版,所以需要更新yum源列表,具体可以参考 http://www.cnblogs.com/tina-s ...