Asp.net MVC 对用户输入的字符串做Trim处理的方法实例


本文摘自PHP中文网,作者零下一度,侵删。

这篇文章主要介绍了Asp.net MVC 如何对所有用户输入的字符串字段做Trim处理,需要的朋友可以参考下

经常需要对用户输入的数据在插入数据库或者判断之前做Trim处理,针对每个ViewModel的字段各自做处理是我们一般的想法。最近调查发现其实也可以一次性实现的。

MVC4.6中实现方式

1,实现IModelBinder接口,创建自定义ModelBinder。

1

2

3

4

5

6

7

8

9

10

public class TrimModelBinder : IModelBinder

  {

    public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)

    {

      var valueResult = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);

      string attemptedValue = valueResult?.AttemptedValue;

 

      return string.IsNullOrWhiteSpace(attemptedValue) ? attemptedValue : attemptedValue.Trim();

    }

  }

2,添加ModelBinder到MVC的绑定库。


1

2

3

4

5

6

7

8

9

protected void Application_Start()

    {

      //System.Web.Mvc.ModelBinders.Binders.DefaultBinder = new ModelBinders.TrimModelBinder();

      System.Web.Mvc.ModelBinders.Binders.Add(typeof(string), new ModelBinders.TrimModelBinder());

      AreaRegistration.RegisterAllAreas();

      FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

      RouteConfig.RegisterRoutes(RouteTable.Routes);

      BundleConfig.RegisterBundles(BundleTable.Bundles);

    }

3,确认一下效果

将密码后面的空格做Trim处理,绑定到ViewModel的时候变成1了:

Asp.net core 1.1 MVC中实现方式

1,自定义ModelBinder并继承ComplexTypeModelBinder


1

2

3

4

5

6

7

8

9

10

11

12

13

public class TrimModelBinder : ComplexTypeModelBinder

  {

    public TrimModelBinder(IDictionary propertyBinders) : base(propertyBinders) { }

  

    protected override void SetProperty(ModelBindingContext bindingContext, string modelName, ModelMetadata propertyMetadata, ModelBindingResult result)

    {

      var value = result.Model as string;

  

      result= string.IsNullOrWhiteSpace(value) ? result : ModelBindingResult.Success(value.Trim());

  

      base.SetProperty(bindingContext, modelName, propertyMetadata, result);

    }

  }

2,为ModelBinder添加自定义Provider


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

public class TrimModelBinderProvider : IModelBinderProvider

  {

    public IModelBinder GetBinder(ModelBinderProviderContext context)

    {

      if (context.Metadata.IsComplexType && !context.Metadata.IsCollectionType)

      {

        var propertyBinders = new Dictionary();

        for (int i = 0; i < context.Metadata.Properties.Count; i++)

        {

          var property = context.Metadata.Properties[i];

          propertyBinders.Add(property, context.CreateBinder(property));

        }

        return new TrimModelBinder(propertyBinders);

      }

      return null;

    }

  }

3,将Provider添加到绑定管理库


1

2

3

4

services.AddMvc().AddMvcOptions(s =>

      {

        s.ModelBinderProviders[s.ModelBinderProviders.TakeWhile(p => !(p is ComplexTypeModelBinderProvider)).Count()] = new TrimModelBinderProvider();

      });

4,确认一下效果

将密码后面的空格做Trim处理,绑定到ViewModel的时候变成1了:

以上就是Asp.net MVC 对用户输入的字符串做Trim处理的方法实例的详细内容!

相关阅读 >>

asp.net core中关于webapi几种版本控制对比详解(图)

mvc页面重定向的asp代码讲解

asp.net core 返回 json datetime 格式

asp.net面试题大全

asp如何解析json字符串并转化为asp对象

asp.net core应用程序在linux上部署的图文详解

实现超时弹窗后跳转功能的asp代码实例

.net的优点

c#实现操作字符串的方法总结

使用asp.net mvc使用jsonresult返回json数据实例详解

更多相关阅读请进入《asp.net》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...