ASP.NET MVC 下自定义模型绑定,去除字符串类型前后的空格
直接贴代码了:
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 下自定义模型绑定,去除字符串类型前后的空格的更多相关文章
- ASP.NET Core 下自定义模型绑定,去除字符串类型前后的空格
效果图: 01 02 直接贴代码了: NoTrim public class NoTrimAttribute : Attribute { } 我们自定义的模型绑定提供程序 /// <summar ...
- Asp.net Mvc 中的模型绑定
asp.net mvc中的模型绑定可以在提交http请求的时候,进行数据的映射. 1.没有模型绑定的时候 public ActionResult Example0() { ) { string id ...
- ASP.NET MVC学习之模型绑定(1)
一.前言 下面我们将开始学习模型绑定,通过下面的知识我们将能够理解ASP.NET MVC模型的模型绑定器是如何将http请求中的数据转换成模型的,其中我们重点讲述的是表单数据. 二.正文 1.简单类型 ...
- ASP.NET MVC 的自定义模型属性别名绑定
最近在研究 ASP.NET MVC 模型绑定,发现 DefaultModelBinder 有一个弊端,就是无法实现对浏览器请求参数的自定义,最初的想法是想为实体模型的属性设置特性(Attribute) ...
- [转]ASP.NET MVC 4 (九) 模型绑定
本文转自:http://www.cnblogs.com/duanshuiliu/p/3706701.html 模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C ...
- ASP.NET MVC 4 (九) 模型绑定
模型绑定指的是MVC从浏览器发送的HTTP请求中为我们创建.NET对象,在HTTP请求和C#间起着桥梁的作用.模型绑定的一个最简单的例子是带参数的控制器action方法,比如我们注册这样的路径映射: ...
- ASP.NET MVC中的模型绑定
模型绑定的本质 任何控制器方法的执行都受action invoker组件(下文用invoker代替)控制.对于每个Action方法的参数,这个invoker组件都会获取一个Model Binder ...
- ASP.NET MVC学习之模型绑定(2)
3.手工调用模型绑定 很多情况下我们都是通过形参的方式接收来自http流中的数据,这看似是完美的,但是缺少了很多过程中的控制,所以我们就需要使用手工的方式进行绑定.下面我们通过一个例子来说明,首先打开 ...
- ASP.NET MVC下自定义错误页和展示错误页的几种方式
在网站运行中,错误是不可避免的,错误页的产生也是不可缺少的. 这几天看了博友的很多文章,自己想总结下我从中学到的和实际中配置的. 首先,需要知道产生错误页的来源,一种是我们的.NET平台抛出的,一种是 ...
随机推荐
- 关于uni-app框架的学习-1
根据官方文档进行学习Uni-APP, 再次过程中,记录一些需要熟悉的内容,有不合适的地方,见着都可知道,----我是一个小白,小白,小白 官网地址:https://uniapp.dcloud.io/ ...
- Linux 桌面玩家指南:10. 没有 GUI 的时候应该怎么玩
特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之 ...
- Linux命令及架构部署大全
1.Linux系统基础知识 Linux 基础优化配置 Linux系统根目录结构介绍 linux系统重要子目录介绍 Linux基础命令(之一)详解 Linux基础命令(之二)详解 Linux文件系统 L ...
- idea中去除重复代码提示的灰色波浪线
可以看到上面代码中的灰色波浪线,特别影响观感,可以看到是因为有了重复代码.不确定它是怎么确定重复代码的. 解决办法: Setting--Editor--Inspections--General---D ...
- WaitGroup
WaitGroup在go语言中,用于线程同步,单从字面意思理解,wait等待的意思,group组.团队的意思,WaitGroup就是指等待一组,等待一个系列执行完成后才会继续向下执行. 先说说Wait ...
- Node.js微服务实践(一)
什么是微服务 微服务是一种架构风格,一个大型复杂软件应用由一个或多个微服务组成.系统中的各个微服务可被独立部署,各个微服务之间是松耦合的.每个微服务仅关注于完成一件任务并很好地完成该任务.在所有情况下 ...
- 使用 Moq 测试.NET Core 应用 -- Mock 方法
第一篇文章, 关于Mock的概念介绍: https://www.cnblogs.com/cgzl/p/9294431.html 本文介绍使用Moq来Mock方法. 使用的代码: https://git ...
- Dubbo简介
1. Dubbo是什么? Dubbo是一个分布式服务框架,致力于提供高性能和透明化的RPC远程服务调用方案,以及SOA服务治理方案.简单的说,dubbo就是个服务框架,如果没有分布式的需求,其实是不需 ...
- Prometheus安装和配置node_exporter监控主机
Node_exporter是可以在* Nix和Linux系统上运行的计算机度量标准的导出器. Node_exporter 主要用于暴露 metrics 给 Prometheus,其中 metrics ...
- Java~命名规范
下面总结以点java命名规范 虽然感觉这些规范比起C#来说有点怪,但还是应该尊重它的命名! 命名规范 项目名全部小写 包名全部小写 类名首字母大写,如果类名由多个单词组成,每个单词的首字母都要大写. ...