直接贴代码了:

SkyModelBinder.cs

using System.ComponentModel;
using System.Linq;
using System.Web.Mvc; namespace MvcSample.Extensions
{
public class SkyModelBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var model = base.BindModel(controllerContext, bindingContext);
if (model is BaseSkyViewModel)
{
((BaseSkyViewModel)model).BindModel(controllerContext, bindingContext);
}
return model;
} protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext,
PropertyDescriptor propertyDescriptor, object value)
{
//check if data type of value is System.String
if (propertyDescriptor.PropertyType == typeof(string))
{
//developers can mark properties to be excluded from trimming with [NoTrim] attribute
if (propertyDescriptor.Attributes.Cast<object>().All(a => a.GetType() != typeof (NoTrimAttribute)))
{
var stringValue = (string)value;
value = string.IsNullOrEmpty(stringValue) ? stringValue : stringValue.Trim();
}
} base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
}
}
}

BaseSkyViewModel.cs

using System.Collections.Generic;
using System.Web.Mvc; namespace MvcSample.Extensions
{
/// <summary>
/// Base sky view model
/// </summary>
[ModelBinder(typeof(SkyModelBinder))]
public partial class BaseSkyViewModel
{
public BaseSkyViewModel()
{
this.CustomProperties = new Dictionary<string, object>();
PostInitialize();
} public virtual void BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
} /// <summary>
/// Developers can override this method in custom partial classes
/// in order to add some custom initialization code to constructors
/// </summary>
protected virtual void PostInitialize()
{ } /// <summary>
/// Use this property to store any custom value for your models.
/// </summary>
public Dictionary<string, object> CustomProperties { get; set; }
} /// <summary>
/// Base Sky entity model
/// </summary>
public partial class BaseSkyEntityViewModel : BaseSkyViewModel
{
public virtual int Id { get; set; }
}
}

NoTrimAttribute.cs

using System;

namespace MvcSample.Extensions
{
/// <summary>
/// Attribute indicating that entered values should not be trimmed
/// </summary>
public class NoTrimAttribute : Attribute
{
}
}

DEMO

ProductInfoModifyViewModel.cs

using MvcSample.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace MvcSample.Models
{
public class ProductInfoModifyViewModel : BaseSkyViewModel
{
public int ProductId { get; set; } /// <summary>
/// 假设有一个这个 Property
/// </summary>
public string ProductName { get; set; } public int NewYear { get; set; }
}
}

HomeController.cs

using MvcSample.Extensions;
using MvcSample.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace MvcSample.Controllers
{
public class HomeController : Controller
{
[HttpPost]
public ActionResult ModifyProduct(ProductInfoModifyViewModel viewModel,
[ModelBinder(typeof(CommaSeparatedModelBinder))] List<int> orderStatusIds = null)
{
ProductInfoService.TryModifyById(viewModel.ProductId, viewModel.NewYear);
return Json(new { Success = true, Message = "保存成功" });
}
}
}

运行截图:

图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. sql xml中 in 的用法

    在xml中,动态传参去数据库查询,下面是in的示例. 比如有条sql SELECT * FROM corp_tax c WHERE c.id in (387419,387423) AND c.corp ...

  2. Maven项目pom文件设置JDK版本

    maven项目创建之后有时候默认设定了java的低版本,每次导入项目或者更新maven的时候可能或出现一些报错,在pom.xml设定版本免除这个问题 <build> <finalNa ...

  3. 【安富莱TCPnet网络教程】HTTP通信实例

    第41章      HTTP超文本传输协议基础知识 本章节为大家讲解HTTP(HyperText Transfer Protocol,超文本传输协议),从本章节开始,正式进入嵌入式Web的设计和学习. ...

  4. SQL中关于不能显示count为0的行的问题

    今天在写自己一个博客项目时遇到了一个数据库问题,因为对于数据库自己所知道的还是很浅显的,对一些查询语句不怎么熟悉. 我目前有一个文章表和评论表,评论表里面有个post_id对应文章表里面的id,想查询 ...

  5. java maven项目update project默认编译器1.5问题解决

    解决办法一:在项目中的pom.xml指定jdk版本,如下 <build> <plugins> <plugin> <groupId>org.apache. ...

  6. docker安装Oracle 12c

    1.安装阿里的docker源: cat /etc/docker/daemon.json { "registry-mirrors": ["https://pee6w651. ...

  7. DateTimeHelper【日期类型与字符串互转以及日期对比相关操作】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 实现日期和字符串之间的转换以及日期的相关操作: 1.日期格式的字符串输出为Date类型: 2.将Date类型以指定格式输出: 3.将 ...

  8. springboot~lombok使用总结

    @Getter & @Setter 生成getter和setter块 @Data注解 @Data相当于@Getter @Setter @RequiredArgsConstructor @ToS ...

  9. 一段JAVA代码了解多线程,JUC、CAS原子性操作。

    @Test public void testPaceController_multiThread() throws InterruptedException { final PaceControlle ...

  10. 2018~第三届南宁市网络安全技术大赛~nnctf~write-up

    Web 1.超简单 分值:100 类型:WEB 已解决 题目:超简单的web题  http://gxnnctf.gxsosec.cn:12311/ 代码审计 <?php $white_list ...