直接贴代码了:

SkyModelBinder.cs

  1. using System.ComponentModel;
  2. using System.Linq;
  3. using System.Web.Mvc;
  4.  
  5. namespace MvcSample.Extensions
  6. {
  7. public class SkyModelBinder : DefaultModelBinder
  8. {
  9. public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  10. {
  11. var model = base.BindModel(controllerContext, bindingContext);
  12. if (model is BaseSkyViewModel)
  13. {
  14. ((BaseSkyViewModel)model).BindModel(controllerContext, bindingContext);
  15. }
  16. return model;
  17. }
  18.  
  19. protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext,
  20. PropertyDescriptor propertyDescriptor, object value)
  21. {
  22. //check if data type of value is System.String
  23. if (propertyDescriptor.PropertyType == typeof(string))
  24. {
  25. //developers can mark properties to be excluded from trimming with [NoTrim] attribute
  26. if (propertyDescriptor.Attributes.Cast<object>().All(a => a.GetType() != typeof (NoTrimAttribute)))
  27. {
  28. var stringValue = (string)value;
  29. value = string.IsNullOrEmpty(stringValue) ? stringValue : stringValue.Trim();
  30. }
  31. }
  32.  
  33. base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
  34. }
  35. }
  36. }

BaseSkyViewModel.cs

  1. using System.Collections.Generic;
  2. using System.Web.Mvc;
  3.  
  4. namespace MvcSample.Extensions
  5. {
  6. /// <summary>
  7. /// Base sky view model
  8. /// </summary>
  9. [ModelBinder(typeof(SkyModelBinder))]
  10. public partial class BaseSkyViewModel
  11. {
  12. public BaseSkyViewModel()
  13. {
  14. this.CustomProperties = new Dictionary<string, object>();
  15. PostInitialize();
  16. }
  17.  
  18. public virtual void BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
  19. {
  20. }
  21.  
  22. /// <summary>
  23. /// Developers can override this method in custom partial classes
  24. /// in order to add some custom initialization code to constructors
  25. /// </summary>
  26. protected virtual void PostInitialize()
  27. {
  28.  
  29. }
  30.  
  31. /// <summary>
  32. /// Use this property to store any custom value for your models.
  33. /// </summary>
  34. public Dictionary<string, object> CustomProperties { get; set; }
  35. }
  36.  
  37. /// <summary>
  38. /// Base Sky entity model
  39. /// </summary>
  40. public partial class BaseSkyEntityViewModel : BaseSkyViewModel
  41. {
  42. public virtual int Id { get; set; }
  43. }
  44. }

NoTrimAttribute.cs

  1. using System;
  2.  
  3. namespace MvcSample.Extensions
  4. {
  5. /// <summary>
  6. /// Attribute indicating that entered values should not be trimmed
  7. /// </summary>
  8. public class NoTrimAttribute : Attribute
  9. {
  10. }
  11. }

DEMO

ProductInfoModifyViewModel.cs

  1. using MvcSample.Extensions;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Web;
  6.  
  7. namespace MvcSample.Models
  8. {
  9. public class ProductInfoModifyViewModel : BaseSkyViewModel
  10. {
  11. public int ProductId { get; set; }
  12.  
  13. /// <summary>
  14. /// 假设有一个这个 Property
  15. /// </summary>
  16. public string ProductName { get; set; }
  17.  
  18. public int NewYear { get; set; }
  19. }
  20. }

HomeController.cs

  1. using MvcSample.Extensions;
  2. using MvcSample.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Web;
  7. using System.Web.Mvc;
  8.  
  9. namespace MvcSample.Controllers
  10. {
  11. public class HomeController : Controller
  12. {
  13. [HttpPost]
  14. public ActionResult ModifyProduct(ProductInfoModifyViewModel viewModel,
  15. [ModelBinder(typeof(CommaSeparatedModelBinder))] List<int> orderStatusIds = null)
  16. {
  17. ProductInfoService.TryModifyById(viewModel.ProductId, viewModel.NewYear);
  18. return Json(new { Success = true, Message = "保存成功" });
  19. }
  20. }
  21. }

运行截图:

图2:

谢谢浏览!

ASP.NET MVC 下自定义模型绑定,去除字符串类型前后的空格的更多相关文章

  1. ASP.NET Core 下自定义模型绑定,去除字符串类型前后的空格

    效果图: 01 02 直接贴代码了: NoTrim public class NoTrimAttribute : Attribute { } 我们自定义的模型绑定提供程序 /// <summar ...

  2. Asp.net Mvc 中的模型绑定

    asp.net mvc中的模型绑定可以在提交http请求的时候,进行数据的映射. 1.没有模型绑定的时候 public ActionResult Example0() { ) { string id ...

  3. ASP.NET MVC学习之模型绑定(1)

    一.前言 下面我们将开始学习模型绑定,通过下面的知识我们将能够理解ASP.NET MVC模型的模型绑定器是如何将http请求中的数据转换成模型的,其中我们重点讲述的是表单数据. 二.正文 1.简单类型 ...

  4. ASP.NET MVC 的自定义模型属性别名绑定

    最近在研究 ASP.NET MVC 模型绑定,发现 DefaultModelBinder 有一个弊端,就是无法实现对浏览器请求参数的自定义,最初的想法是想为实体模型的属性设置特性(Attribute) ...

  5. [转]ASP.NET MVC 4 (九) 模型绑定

    本文转自:http://www.cnblogs.com/duanshuiliu/p/3706701.html 模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C ...

  6. ASP.NET MVC 4 (九) 模型绑定

    模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C#间起着桥梁的作用.模型绑定的一个最简单的例子是带参数的控制器action方法,比如我们注册这样的路径映射: ...

  7. ASP.NET MVC中的模型绑定

    模型绑定的本质   任何控制器方法的执行都受action invoker组件(下文用invoker代替)控制.对于每个Action方法的参数,这个invoker组件都会获取一个Model Binder ...

  8. ASP.NET MVC学习之模型绑定(2)

    3.手工调用模型绑定 很多情况下我们都是通过形参的方式接收来自http流中的数据,这看似是完美的,但是缺少了很多过程中的控制,所以我们就需要使用手工的方式进行绑定.下面我们通过一个例子来说明,首先打开 ...

  9. ASP.NET MVC下自定义错误页和展示错误页的几种方式

    在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...

随机推荐

  1. 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现

    1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...

  2. Java中Map和Object的互相转换方式

    一.使用Apache中的BeanUtils类,导入commons-beanutils包. Jar包Maven下载地址:https://mvnrepository.com/artifact/common ...

  3. Mybatis之旅第三篇-SqlMapConfig.xml全局配置文件解析

    一.前言 刚换工作,为了更快的学习框架和了解业务,基本每天都会加班,导致隔了几天没有进行总结,心里总觉得不安,工作年限越长越感到学习的重要性,坚持下去!!! 经过前两篇的总结,已经基本掌握了mybat ...

  4. java~springboot~ibatis数组in查询的实现

    在ibatis的xml文件里,我们去写sql语句,对应mapper类的方法,这些sql语句与控制台上没什么两样,但在有些功能上需要注意,如where in这种从数组里查询符合条件的集合里,需要在xml ...

  5. Fescar(Seata)-Springcloud流程分析-1阶段

    Fescar是阿里18年开源的分布式事务的框架.Fescar的开源对分布式事务框架领域影响很大.作为开源大户,Fescar来自阿里的GTS,经历了好几次双十一的考验,一经开源便颇受关注.今天就来看了F ...

  6. k8s网络之Flannel网络

    k8s网络主题系列: 一.k8s网络之设计与实现 二.k8s网络之Flannel网络 三.k8s网络之Calico网络 简介 Flannel是CoreOS团队针对Kubernetes设计的一个网络规划 ...

  7. GoLang simple-project-demo-01

    Hello world 经典例子: package main import "fmt" func main(){ fmt.Println("hello world&quo ...

  8. 原生js轮盘抽奖实例分析(幸运大转盘抽奖)

    效果图: 所需图片素材: 这张图是pointer.png的位置的. turntable-bg.jpg这张是转盘背景图,在背景位置. 这张是turntable.png位置的. 需要这三张图片,如果要实现 ...

  9. c#中缓存的使用

    缓存的使用: 缓存是分布式系统中的重要组件,主要解决高并发,大数据场景下,热点数据访问的性能问题.提供高性能的数据快速访问,提高数据的读取速度.因为服务器和应用客户端之间存在着流量的瓶颈,所以读取大容 ...

  10. Struts2中五个重要的常量

    一.五个常量的位置:位于xwork核心包下的Action字节码文件里 二.五个常量的介绍: a: SUCCESS public static final String SUCCESS = " ...