如何修剪MVC中的所有值(添加和编辑时) [英] How to trim all value in MVC (while add and edit)

查看:77
本文介绍了如何修剪MVC中的所有值(添加和编辑时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

修改MVC控制器中所有值的任何快捷方式。



我尝试过的方法:



NO。目前,所有字段都使用修剪。

Any shortcut for trim all value in MVC controller.

What I have tried:

NO. currently, all fields are using a trim.

推荐答案

创建一个类



Make one class

public class TrimModelBinder : System.Web.Mvc.DefaultModelBinder
   {
       protected override void SetProperty(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext,System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
       {
           if (propertyDescriptor.PropertyType == typeof(string))
           {
               var stringValue = (string)value;
               if (!string.IsNullOrWhiteSpace(stringValue))
               {
                   value = stringValue.Trim();
               }
               else
               {
                   value = null;
               }
           }
           base.SetProperty(controllerContext, bindingContext,
                               propertyDescriptor, value);
       }
   }







//global.asax Application_Start event.

ModelBinders.Binders.DefaultBinder = new TrimModelBinder();







asp.net - 数据输入后修剪字符串的最佳方法。我应该创建自定义模型绑定器吗? - 堆栈溢出 [ ^ ]


这篇关于如何修剪MVC中的所有值(添加和编辑时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆