ASP.NET MVC 下自定义模型绑定,去除字符串类型前后的空格
2018-12-19 18:46 音乐让我说 阅读(902) 评论(0) 编辑 收藏 举报直接贴代码了:
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:
谢谢浏览!
作者:音乐让我说(音乐让我说 - 博客园)
出处:http://music.cnblogs.com/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。