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. Dos下查询关闭端口的命令例子

    1. 查看端口占用 在windows命令行窗口下执行: netstat -aon|findstr "8080" TCP     127.0.0.1:80         0.0.0 ...

  2. cpoint

    #include<iostream> #include<math.h> using namespace std; class CPoint { public: int cpoi ...

  3. C++11智能指针

    今晚跟同学谈了一下智能指针,突然想要看一下C++11的智能指针的实现,因此下了这篇博文. 以下代码出自于VS2012 <memory> template<class _Ty> ...

  4. 【风马一族_Python】 实施kNN算法

    一.在PyCharm 5.0.4(编写python程序的IDE) 编写kNN.py文件的代码 -------------------------- 1. kNN.py  运算符模块 --------- ...

  5. FPGA笔记-读取.dat文件

    读取.dat图像文件 .dat文件是matlab生成的图像文件 initial begin // Initialize Inputs CLK = 0; RST = 1; IMAGE_DATA = 0; ...

  6. 将word中的“空格” 转换为换行符

  7. linux下文件的复制、移动与删除

    linux下文件的复制.移动与删除命令为:cp,mv,rm 一.文件复制命令cp     命令格式:cp [-adfilprsu] 源文件(source) 目标文件(destination)      ...

  8. Win7构造wifi热点【Written By KillerLegend】

    1:Win+R打开DOS,输入以下命令然后回车,不要关闭该DOS窗口: netsh wlan set hostednetwork  mode = allow ssid =你的wifi热点名字  key ...

  9. require.js入门指南(一)

    *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...

  10. 键盘样式风格有关设置-iOS开发

    一.键盘风格 UIKit框架支持8种风格键盘. typedef  enum  { UIKeyboardTypeDefault,                 // 默认键盘:支持所有字符 UIKey ...