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平台抛出的,一种是 ...
随机推荐
- 深度优先搜索(DFS)与广度优先搜索(BFS)的Java实现
1.基础部分 在图中实现最基本的操作之一就是搜索从一个指定顶点可以到达哪些顶点,比如从武汉出发的高铁可以到达哪些城市,一些城市可以直达,一些城市不能直达.现在有一份全国高铁模拟图,要从某个城市(顶点) ...
- Java中Map和Object的互相转换方式
一.使用Apache中的BeanUtils类,导入commons-beanutils包. Jar包Maven下载地址:https://mvnrepository.com/artifact/common ...
- Mybatis之旅第三篇-SqlMapConfig.xml全局配置文件解析
一.前言 刚换工作,为了更快的学习框架和了解业务,基本每天都会加班,导致隔了几天没有进行总结,心里总觉得不安,工作年限越长越感到学习的重要性,坚持下去!!! 经过前两篇的总结,已经基本掌握了mybat ...
- java~springboot~ibatis数组in查询的实现
在ibatis的xml文件里,我们去写sql语句,对应mapper类的方法,这些sql语句与控制台上没什么两样,但在有些功能上需要注意,如where in这种从数组里查询符合条件的集合里,需要在xml ...
- Fescar(Seata)-Springcloud流程分析-1阶段
Fescar是阿里18年开源的分布式事务的框架.Fescar的开源对分布式事务框架领域影响很大.作为开源大户,Fescar来自阿里的GTS,经历了好几次双十一的考验,一经开源便颇受关注.今天就来看了F ...
- k8s网络之Flannel网络
k8s网络主题系列: 一.k8s网络之设计与实现 二.k8s网络之Flannel网络 三.k8s网络之Calico网络 简介 Flannel是CoreOS团队针对Kubernetes设计的一个网络规划 ...
- GoLang simple-project-demo-01
Hello world 经典例子: package main import "fmt" func main(){ fmt.Println("hello world&quo ...
- 原生js轮盘抽奖实例分析(幸运大转盘抽奖)
效果图: 所需图片素材: 这张图是pointer.png的位置的. turntable-bg.jpg这张是转盘背景图,在背景位置. 这张是turntable.png位置的. 需要这三张图片,如果要实现 ...
- c#中缓存的使用
缓存的使用: 缓存是分布式系统中的重要组件,主要解决高并发,大数据场景下,热点数据访问的性能问题.提供高性能的数据快速访问,提高数据的读取速度.因为服务器和应用客户端之间存在着流量的瓶颈,所以读取大容 ...
- Struts2中五个重要的常量
一.五个常量的位置:位于xwork核心包下的Action字节码文件里 二.五个常量的介绍: a: SUCCESS public static final String SUCCESS = " ...