Asp.net Mvc 中的模型绑定
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 中的模型绑定的更多相关文章
- ASP.NET MVC中的模型绑定
模型绑定的本质 任何控制器方法的执行都受action invoker组件(下文用invoker代替)控制.对于每个Action方法的参数,这个invoker组件都会获取一个Model Binder ...
- ASP.NET MVC学习之模型绑定(1)
一.前言 下面我们将开始学习模型绑定,通过下面的知识我们将能够理解ASP.NET MVC模型的模型绑定器是如何将http请求中的数据转换成模型的,其中我们重点讲述的是表单数据. 二.正文 1.简单类型 ...
- ASP.NET MVC中的模型装配 封装方法 非常好用
下面说一下 我们知道在asp.net mvc中 视图可以绑定一个实体模型 然后我们三层架构中也有一个model模型 但是这两个很多时候却是不一样的对象来的 就拿微软的官方mvc例子来说明 微软的视图实 ...
- ASP.NET Core 中的模型绑定
微软官方文档:ASP.NET Core 中的模型绑定 Route 是通过MVC Route URL取值. 如:http://localhost:5000/Home/Index/2,id取出的值就会是2 ...
- ASP.NET MVC学习之模型绑定(2)
3.手工调用模型绑定 很多情况下我们都是通过形参的方式接收来自http流中的数据,这看似是完美的,但是缺少了很多过程中的控制,所以我们就需要使用手工的方式进行绑定.下面我们通过一个例子来说明,首先打开 ...
- [转]ASP.NET MVC 4 (九) 模型绑定
本文转自:http://www.cnblogs.com/duanshuiliu/p/3706701.html 模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C ...
- ASP.NET MVC 4 (九) 模型绑定
模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C#间起着桥梁的作用.模型绑定的一个最简单的例子是带参数的控制器action方法,比如我们注册这样的路径映射: ...
- ASP.NET MVC 下自定义模型绑定,去除字符串类型前后的空格
直接贴代码了: SkyModelBinder.cs using System.ComponentModel; using System.Linq; using System.Web.Mvc; name ...
- ASP.NET MVC中默认Model Binder绑定Action参数为List、Dictionary等集合的实例
在实际的ASP.NET mvc项目开发中,有时会遇到一个参数是一个List.Dictionary等集合类型的情况,默认的情况ASP.NET MVC框架是怎么为我们绑定ASP.NET MVC的Actio ...
随机推荐
- My97DatePicker使用的问题
我在iframe中使用My97DatePicker时,发现第一次点击左边的菜单时,在右边的页面可以弹出日期框: 当我第二次点击菜单时,右边的日期文本框却弹出了页面的内容,而不是日期选择框: 首先怀疑是 ...
- SQL Server中的PWDENCRYPT与PWDCOMPARE函数
前幾天有個客戶的網站出問題(不是我們開發的),請我們幫他看,主要的問題是他們的網站會員在進行查詢密碼時,會員收到信的時候在密碼的欄位竟然會出現 System.Binary[] 字樣.而我進去資料庫中查 ...
- JQuery>>>get/post赋值全局变量
JQuery' post&get: asynchronous. 2014-06-27 16:57:25 var client; function getClient(){ $.ajax ...
- 程序员都会的 35 个 jQuery 小技巧
收集的35个 jQuery 小技巧/代码片段,可以帮你快速开发. 1. 禁止右键点击 $(document).ready(function(){ $(document).bind("cont ...
- python运维开发(七)----面向对象(上)
内容目录: 面向对象应用场景 类和对象的创建 类中的__init__构造方法 self理解 面向对象的三大特性:封装.继承.多态 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装 ...
- SignalR实现实时日志监控
.net SignalR实现实时日志监控 摘要 昨天吃饭的时候,突然想起来一个好玩的事,如果能有个页面可以实时的监控网站或者其他类型的程序的日志,其实也不错.当然,网上也有很多成熟的类似的监控系统 ...
- gdal vc++ 配置说明
1在VC中,打开菜Tool-Option,在Directories页面中的Library files中和Include files中分别添加GDAL的LIB文件目录和INCLUDE文件目录2打开菜 ...
- 用 for/in 在 Java 5.0 中增强循环
这个方便的构造提供了什么?什么时候适用于代码? Brett McLaughlin (brett@newInstance.com), 作者/编辑, O'Reilly Media, Inc. 简介: fo ...
- VMware网络模式介绍(下篇)
VMware网络模式介绍 VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式). Bridged 模式: 在桥接模式下,VMwa ...
- MVC折线图应用
后台 获取值并转换成json数据存到实体里面,然后前台输出 HighchartsModels model = new HighchartsModels(); model.DataDicJson = J ...