Asp.net MVC如何对输入的字符串字段做Trim处理

发布时间:2021-09-13 17:43:17 作者:小新
来源:亿速云 阅读:125

这篇文章将为大家详细讲解有关Asp.net MVC如何对输入的字符串字段做Trim处理,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

MVC4.6中实现方式

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

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的绑定库。

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,确认一下效果

Asp.net MVC如何对输入的字符串字段做Trim处理

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

Asp.net MVC如何对输入的字符串字段做Trim处理

Asp.net core 1.1 MVC中实现方式

1,自定义ModelBinder并继承ComplexTypeModelBinder

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

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添加到绑定管理库

services.AddMvc().AddMvcOptions(s =>
      {
        s.ModelBinderProviders[s.ModelBinderProviders.TakeWhile(p => !(p is ComplexTypeModelBinderProvider)).Count()] = new TrimModelBinderProvider();
      });

4,确认一下效果

Asp.net MVC如何对输入的字符串字段做Trim处理

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

Asp.net MVC如何对输入的字符串字段做Trim处理

关于“Asp.net MVC如何对输入的字符串字段做Trim处理”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. Asp.net MVC应该如何对用户输入的字符串做Trim处理
  2. ASP.NET中过滤器、模型绑定的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

asp.net trim 字符串

上一篇:什么是freeingitems

下一篇:如何使用php foreach修改值

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》