直接贴代码了:

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. LeetCode算法题-Minimum Distance Between BST Nodes(Java实现-四种解法)

    这是悦乐书的第314次更新,第335篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第183题(顺位题号是783).给定具有根节点值的二叉搜索树(BST),返回树中任何两个 ...

  2. ipv6地址配置实验(GNS3/ENSP)

    实验拓扑: IPV6地址配置如图所示, 配置ipv6指令(以R2为例,R1类似): int e1/2 R2(config-if)#ipv6 address 2001:db08:acad:1::2/64 ...

  3. 快速构建SPA框架SalutJS--项目工程目录 三

    配置文件 在开始我们的第一个界面之前,我们需要把初始的html和config文件配置好.html非常简单,只需要一个div作为最外部的容器包裹着所有界面即可: <!DOCTYPE html> ...

  4. [Inside HotSpot] Java的方法调用

    1. 方法调用模块入口 Java所有的方法调用都会经过JavaCalls模块.该模块又细分为call_virtual调用虚函数,call_static调用静态函数等.虚函数调用会根据对象类型进行方法决 ...

  5. C/C++性能测试工具GNU gprof

    代码剖析(Code profiling)程序员在优化软件性能时要注意应尽量优化软件中被频繁调用的部分,这样才能对程序进行有效优化.使用真实的数据,精确的分析应用程序在时间上的花费的行为就成为_代码剖析 ...

  6. Docker & ASP.NET Core (4):容器间的连接

    第一篇:把代码连接到容器 第二篇:定制Docker镜像 第三篇:发布镜像 Docker容器间的连接 Docker提供了两种方式可以用来做容器间的连接/通信: Legacy Linking:这种方式使用 ...

  7. 使用JDBC连接操作数据库

    JDBC简介 Java数据库连接(Java Database Connectivity,JDBC),是一种用于执行SQL语句的Java API,它由一组用Java编程语言编写的类和接口组成. JDBC ...

  8. Keras Model Sequential模型接口

    Sequential 模型 API 在阅读这片文档前,请先阅读 Keras Sequential 模型指引. Sequential 模型方法 compile compile(optimizer, lo ...

  9. java爬虫系列第二讲-爬取最新动作电影《海王》迅雷下载地址

    1. 目标 使用webmagic爬取动作电影列表信息 爬取电影<海王>详细信息[电影名称.电影迅雷下载地址列表] 2. 爬取最新动作片列表 获取电影列表页面数据来源地址 访问http:// ...

  10. 请收好这份NLP热门词汇解读

    文章发布于公号[数智物语] (ID:decision_engine),关注公号不错过每一篇干货. 来源 | 微软研究院AI头条 编者按:在过去的一段时间,自然语言处理领域取得了许多重要的进展,Tran ...