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 ...
随机推荐
- MYSQL SET类型字段的SQL查询某个字段保函某个值的查询
1.column set('hot','crazy','smart') //column字段(set属性)三个值 2.select * from table where FIND_IN_SET('h ...
- MySQL server has gone away报错
1.最近做插入数据库,然后一直报一个错.mysql server has gone away.(如下图) 查了好多资料,终于解决了.. 1.可能是连接超时..进入php.ini,修改wait_time ...
- 新测 thinkphp3.2 整合 PHPMailer 发送邮件
PHPMailer 的官方网站:http://phpmailer.worxware.com/ PHPMailer 最新类库下载地址:[点击下载] PHPMailer GitHub 下载地址:https ...
- php array 排序 感悟
array 排序总体有这几个函数sort.rsort.asort.arsort.ksort.krsort.usort.uasort.uksort. 一开始我记来记去总是有点混乱,后来认真对比后终于清 ...
- Objective-C实现计算你写了多少行代码
#import <Foundation/Foundation.h> NSInteger codeLineCount(NSString *path) { //1.获取文件管理者,这是一个单例 ...
- 如何让ios app支持32位和64位?
将ios app转换为兼容32位和64位步骤: 1. 安装 Xcode 5. 2. 打开你的项目.Xcode会提示你更新你的项目,其中的警告和错误信息对于转换到64位相当重要. 3. 将你的项目 ...
- linux之普通用户与root用户之间切换
- C Statements
1,while((ch = getchar()) != EOF){ putchar(ch);}2,while((ch=getchar()) != EOF){ if(ch < '0' ...
- #include <boost/weak_ptr.hpp>
弱指针boost::weak_ptr的定义在boost/weak_ptr.hpp里.到目前为止介绍的各种智能指针都能在不同的场合下独立使用.相反,弱指针只有在配合共享指针一起使用时才有意义.因此弱指针 ...
- magento产品导入时需要注意的事项
(1) 必须保证csv文件是utf-8编码的.非utf-8的编码会导致产品导入失败 (2)产品图片 (a) 产品图片必须包含image,image_label,_media_image,_media_ ...