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平台抛出的,一种是 ...
随机推荐
- Nginx反向代理解决iframe跨域问题
前言 这几天有个需求:做个表单页面,要求后台人员能自定义发布表单,用户来填写表单.我一想,这不麦克表单有现成的吗,拿来就用!发布表单后,可以选择使用iframe方式嵌入网站,一切顺利. 当时的网站是h ...
- Mysql中concat()、concat_ws()和 group_concat()的用法
一.CONCAT()函数CONCAT()函数用于将多个字符串连接成一个字符串.使用数据表Info作为示例,其中SELECT id,name FROM info LIMIT 1;的返回结果为+----+ ...
- Luogu P5285 [十二省联考2019]骗分过样例
Preface ZJOI一轮被麻将劝退的老年选手看到这题就两眼放光,省选也有乱搞题? 然后狂肝了3~4天终于打完了,期间还补了一堆姿势 由于我压缩技术比较菜,所以用的都是非打表算法,所以一共写了5K- ...
- Java引用详解-StrongReference SoftReference WeakReference PhantomReference
1 Java引用介绍 Java从1.2版本开始引入了4种引用,这4种引用的级别由高到低依次为: 强引用 > 软引用 > 弱引用 > 虚引用 ⑴强引用(StrongR ...
- Linux 使用 free 命令查看内存使用情况
1.free 命令的选项 使用 free 命令查看服务器内存使用情况. free [-b|-k|-m|-g|-h] [-l] [-o] [-t] [-s delay] [-c count] [-V] ...
- ES 06 - 通过Kibana插件增删改查ES中的索引文档
目录 1 document的结构 2 document的常见CRUD操作 2.1 添加商品: 添加文档并建立索引 2.2 查询商品: 检索文档 2.3 修改商品: 替换文档 2.4 修改商品: 更新文 ...
- Kubernetes的DaemonSet(上篇)
背景 静儿作为美团容器化团队HULK的一员,经常需要和Kubernetes(k8s)打交道.第一次登陆node(宿主机)的时候,发现连续登陆几台都看到了Prometheus-Node-Exporter ...
- springboot~@Valid注解对嵌套类型的校验
@Valid注解可以实现数据的验证,你可以定义实体,在实体的属性上添加校验规则,而在API接收数据时添加@valid关键字,这时你的实体将会开启一个校验的功能,具体的代码如下,是最基本的应用: 实体: ...
- 访问了一次百度网页,你都经历了什么?https及tcp协议揭秘
打开https://www.baidu.com/ 网页一个简单的动作,都经历了什么?你想探究内部的原理吗?那我们一起去探索吧 1.准备工作 安装好wireshark.Wireshark(前称Ether ...
- WIN10安装不上IIS,使用IISExpress作为发布服务
[背景] 本人开发Win程序,需要调用网站资源作为Win程序的辅助功能,为此需要本地开发环境支持IIS.最近重装系统,VS安装完后,接着再安装IIS,可以在添加删除程序中反复尝试,均告安装失败提示.最 ...