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平台抛出的,一种是 ...
随机推荐
- Daily Life 01
2019-03-03 我不擅于用文字记录自己的生活,因为很长时间一个人习惯了随意简单的生活,觉得很多事留给回忆就好,另一方面文笔不好,怕自己流出的文字不有趣,过于流水.有看过一些身边人写的随记,都有写 ...
- NumPy的基本用法
NumPy简介:NumPy是高性能科学计算和数据分析的基础包.是pandas等其他各种工具的基础NumPy主要功能:ndarray,一个多维数组结构,高效且节省空间无需循环对数组数据进行快速运算的数学 ...
- Project file is incomplete. Expected imports are missing 错误解决方案
当你打开一个.net core的项目,Visual Studio 可能无法打开,提示如下错误: D:\workshop\Github\Ocelot\src\Ocelot\Ocelot.csproj : ...
- ASP.NET Core 使用 SignalR 遇到的 CORS 问题
问题 将 SignalR 集成到 ASP.NET Core MVC 程序的时候,按照官方 DEMO 配置完成,但使用 DEMO 页面建立连接一直提示如下信息. Access to XMLHttpReq ...
- C++STL之Vector向量详解,用法和例子 一起学习 一起加油
C++ STL之vector用法总结 1 ...
- 【ASP.NET Core快速入门】(三)准备CentOS和Nginx环境
基本软件 VMware虚拟机 centos:http://isoredirect.centos.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-170 ...
- sql的查询语句的总结
一:基本的查询sql 1:基本常用查询 select * from student; --select select all sex from student; --all 查询所有 select d ...
- Memcache的 简介
MemCache memcache是一套分布式的高速缓存系统.目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的.需要频繁访问数据库的网站访问速度提升效果十分显著,是一套开放源代码软件. 工作 ...
- [转帖]无网络离线安装 vs2017
无网络离线安装 vs2017 公司电脑禁止,只有一个老的vs2017的安装目录(之前通过 --layout 安装时生成的离线文件).找了一圈百度,没能解决问题,最后,问bing,查微软的官方网站命令, ...
- 第三章:shiro授权认证
授权:也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等). 主体:即访问应用的用户,在Shiro中使用Subject代表该用户.用户只有授权后才允许访问相应的资源. 资源 ...