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. [Spring Boot Reference Guide] 读书笔记一 Getting Started

    8. Introducing Spring Boot Goals of spring boot: Provide a radically faster and widely accessible ge ...

  2. C# 与MySQL

    1. MySQL.Data.dll       http://files.cnblogs.com/files/lwngreat/MySql.Data.rar 2.在工程中添加引用 3. 使用  Mys ...

  3. opals 开发记录

    1:开发需要的文件 下载地址:http://www.geo.tuwien.ac.at/opals/html/index.html 注意只能在release下才能通过. 我自己整理好的(64位的),以备 ...

  4. python版去UTF-8 BOM

    今天给app弄银联支付接口.直接copy银联的sdk.结果.安卓和ios始终报json格式错误.找了半天.都没找到问题.最后怀疑可能是BOM破坏了json的数据格式转换.验证后确认是BOM的问题.为方 ...

  5. C++ 包含头文件 和 宏的使用 和 条件编译

    1 #define命令剖析 1.1   #define的概念     #define命令是C语言中的一个宏定义命令,它用来将一个标识符定义为一个字符串,该标识符被称为宏名,被定义的字符串称为替换文本. ...

  6. 有趣的IT面试题

    一段看起来很简单C代码,预期结果是输出array数组. #include<stdio.h> #define TOTAL_ELEMENTS (sizeof(array) / sizeof(a ...

  7. web安全测试工具介绍---webscarab

    webscarab: 这主要是一款代理软件或许没有其它的工具能和OWASP的WebScarab如此丰富的功能相媲美了,如果非要列举一些有用的模块的话,那么他们包括HTTP代理,网络爬行.网络蜘蛛,会话 ...

  8. gdal vc++ 配置说明

      1在VC中,打开菜Tool-Option,在Directories页面中的Library files中和Include files中分别添加GDAL的LIB文件目录和INCLUDE文件目录2打开菜 ...

  9. Visual Studio 2015 Update 3 RC 候选预览版粗来了

    .Net 基金会 http://www.dotnetfoundation.org/ 更新的真快,刚打完2的补丁包,3就粗来了............ https://www.visualstudio. ...

  10. Web常用函数介绍(LoadRunner相关)

    介绍大纲:1. web_url2. web_image3. web_link4. web_submmit_form 详细介绍: 一. web_url web_url 语法: Int Web_url(c ...