1、Model Binder从哪些地方获取数据(找到数据后会停止继续寻找)

MVC 框架内置默认的 Model Binder 是 DefaultModelBinder 类。当 Action Invoker 没找到自定义的 Binder 时,则默认使用 DefaultModelBinder。默认情况下,DefaultModelBinder 从如下 4 种途径查找要绑定到 Model 上的值:

  1. Request.Form,HTML form 元素提供的值。
  2. RouteData.Values,通过应用程序路由提供的值。
  3. Request.QueryString,所请求 URL 的 query string 值。
  4. Request.Files,客户端上传的文件。

如果是HttpGet:一般是从RouteData.Values获取路由的值

如果是HttpPost:一般是从Request.Form获取路由的值

2、绑定复合类型

绑定复合类型时会一一绑定复合类型的公共属性

也可以绑定数组:通过相同的Name属性绑定

也可以绑定到集合,具体参考:

http://www.cnblogs.com/willick/p/3424188.html#header_1

3、手动调用绑定(UpdateModel方法)

public ActionResult Address() {
IList<Address> addresses = new List<Address>();
UpdateModel(addresses, new FormValueProvider(ControllerContext));
return View(addresses);
}

手动绑定即把从各种途径(主要由4个途径)获取的数据手动和Model进行绑定

4、自定义Model Binding(自定义绑定可对数据进行过滤及操作)

  4.1 继承于DefaultModelBinder

    

 public class MyModelBinder : DefaultModelBinder
{ protected override void SetProperty(ControllerContext controllerContext,
ModelBindingContext bindingContext,
System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
{ trim(propertyDescriptor, ref value);
antiSqlInject(propertyDescriptor, ref value); base.SetProperty(controllerContext, bindingContext,
propertyDescriptor, value);
} /// <summary>
/// 去除两边空格
/// </summary>
/// <param name="propertyDescriptor"></param>
/// <param name="value"></param>
private void trim(System.ComponentModel.PropertyDescriptor propertyDescriptor, ref object value)
{
if (propertyDescriptor.PropertyType == typeof(string))
{
var stringValue = (string)value;
if (!string.IsNullOrWhiteSpace(stringValue))
{
stringValue = stringValue.Trim();
} value = stringValue;
}
} /// <summary>
/// 防止SQL注入
/// </summary>
/// <param name="propertyDescriptor"></param>
/// <param name="value"></param>
private void antiSqlInject(System.ComponentModel.PropertyDescriptor propertyDescriptor, ref object value)
{
if (propertyDescriptor.PropertyType == typeof(string))
{
var stringValue = (string)value;
if (!string.IsNullOrWhiteSpace(stringValue))
{
stringValue = AntiInjectUtil.StripAllTags(stringValue);
} value = stringValue;
}
}

将默认的ModelBinder(DefaultModelBinder)替换为自定义的ModelBinder,作用于为全局

   protected void Application_Start()
{
ModelBinders.Binders.DefaultBinder = new MyModelBinder();
}

4.2 继承于IModelBinder

public class AddressBinder : IModelBinder {
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
Address model = (Address)bindingContext.Model ?? new Address();
model.City = GetValue(bindingContext, "City");
model.Country = GetValue(bindingContext, "Country");
return model;
} private string GetValue(ModelBindingContext context, string name) {
name = (context.ModelName == "" ? "" : context.ModelName + ".") + name;
ValueProviderResult result = context.ValueProvider.GetValue(name);
if (result == null || result.AttemptedValue == "")
return "<Not Specified>";
else
return (string)result.AttemptedValue;
}
}

注册ModelBinder

protected void Application_Start()
{ ModelBinders.Binders.Add(typeof(Address), new AddressBinder());
}

MVC3 ModelBinder的更多相关文章

  1. 分享在MVC3.0中使用jQuery DataTable 插件

    前不久在网络上看见一个很不错的jQuery的DataTable表格插件.后来发现在MVC中使用该插件的文章并不多.本文将介绍在MVC3.0如何使用该插件.在介绍该插件之前先简单介绍一下,推荐该插件的原 ...

  2. Ubuntu(Linux) + mono + xsp4 + nginx +asp.net MVC3 部署

    折腾了一下,尝试用Linux,部署mvc3. 分别用过 centos 和 ubuntu ,用ubuntu是比较容易部署的. 操作步骤如下: 一.终端分别如下操作 sudo su ->输入密码 a ...

  3. ASP.NET MVC5 ModelBinder

    什么是ModelBinding ASP.NET MVC中,所有的请求最终都会到达某个Controller中的某个Action并由该Action负责具体的处理和响应.为了能够正确处理请求,Action的 ...

  4. vs2010如何安装mvc3,怎样安装,详细的步骤,从哪下载?请看这篇文章。

    vs2010如何安装mvc3,怎样安装,详细的步骤,从哪下载?请看这篇文章. 安装步骤:vs2010 -> vs2010sp1 -> AspNetMVC3Setup -> AspNe ...

  5. ASP.NET MVC3中Controller与View之间的数据传递总结

    一.  Controller向View传递数据 1.       使用ViewData传递数据 我们在Controller中定义如下: ViewData["Message_ViewData& ...

  6. ASP.NET MVC3中Controller与View之间的数据传递

    在ASP.NET MVC中,经常会在Controller与View之间传递数据,因此,熟练.灵活的掌握这两层之间的数据传递方法就非常重要.本文从两个方面进行探讨: 一.  Controller向Vie ...

  7. Log4Net异常日志记录在asp.net mvc3.0的应用

    前言 log4net是.Net下一个非常优秀的开源日志记录组件.log4net记录日志的功能非常强大.它可以将日志分不同的等级,以不同的格式,输出到不同的媒介.本文主要是简单的介绍如何在Visual ...

  8. 【记录】VS2012新建MVC3/MVC4项目时,报:此模板尝试加载组件程序集“NuGet.VisualStudio.Interop...”

    最近电脑装了 VisualStudio "14" CTP,由于把其他版本的 VS 卸掉,由高到低版本安装,当时安装完 VisualStudio "14" CTP ...

  9. Linux(CentOS 6.7)下配置Mono和Jexus并且部署ASP.NET MVC3、4、5和WebApi(跨平台)

    1.开篇说明 a. 首先我在写这篇博客之前,已经在自己本地配置了mono和jexus并且成功部署了asp.net mvc项目,我也是依赖于在网上查找的各种资料来配置环境并且部署项目的,而其在网上也已有 ...

随机推荐

  1. INSERT IGNORE 与 INSERT INTO的区别

    例 insert ignore表示,如果中已经存在相同的记录,则忽略当前新数据: insert ignore into table(name)  select  name from table2 例 ...

  2. iOS-NSOperation多线程

    NSOperation 一.简介 1.使用步骤 配合使用NSOperation和NSOperationQueue也能实现多线程编程 先将操作封装到一个NSOperation对象中 然后将NSOpera ...

  3. 对象this、currentTarget和target

    在事件处理程序内部,对象this始终等于currentTarget的值,而target则只包含事件的实际目标.如果直接将事件处理程序指定给了目标元素,则this.currentTarget和targe ...

  4. 7款个性化jQuery/HTML5地图插件

    现在我们经常会用到一些地图应用,无论是在网页上还是手机App中,地图貌似是一个不可或缺的应用.本文将带领大家一起来看看一些基于jQuery和HTML5的个性化地图插件,有几款地图比较实用,有些则是具有 ...

  5. 杭电ACM2011-- 多项式求和

    题目地址 :多项式求和 /* #include<stdio.h> int main() { int n,b; double a[110],x; double z; int i,j; int ...

  6. Cstring使用说明

    CString::Left(intnCount)const; //从左边1开始获取前 nCount个字符 CString::Mid(intnFirst)const; //从左边第 nCount+1个字 ...

  7. int * const 与 const int * 的区别

    type * const 与 const type * 是在C/C++编程中特别容易混淆的两个知识点,现在就以 int * const 和 const int * 为例来简略介绍一下这两者之间的区别. ...

  8. python: 生成guid

    其实经常需要生成一个guid,在各种场合使用...也简单写个小脚本实现吧. 实现下来发现速度比较慢... import uuid import sys def show_ver(): print 'g ...

  9. VB最新使用教程

    Visual Basic是一种由 微软公司开发的结构化的.模块化的.面向对象的.包含协助开发环境的事件驱动为机制的可视化程序设计语言.这是一种可用于微软自家产品开发的语言.它源自于BASIC编程语言. ...

  10. 小课堂Week9 例外处理设计的逆袭Part2

    小课堂Week9 例外处理设计的逆袭Part2 今天继续阅读<例外处理设计的逆袭>这本书,我们先看两个案例: 案例1 问:如果要设计一个依据学号到数据库中查询学生资料的函数,当找不到符合条 ...