Asp.net MVC 对所有用户输入的字符串字段做Trim处理的方法

2025-05-29 0 55

经常需要对用户输入的数据在插入数据库或者判断之前做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,确认一下效果

Asp.net MVC 对所有用户输入的字符串字段做Trim处理的方法

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

Asp.net MVC 对所有用户输入的字符串字段做Trim处理的方法

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

Asp.net MVC 对所有用户输入的字符串字段做Trim处理的方法

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

Asp.net MVC 对所有用户输入的字符串字段做Trim处理的方法

以上所述是小编给大家介绍的Asp.net MVC 对所有用户输入的字符串字段做Trim处理的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

原文链接:http://www.cnblogs.com/lixiaobin/p/AspnetMVCTrim.html

收藏 (0) 打赏

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

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Asp.net MVC 对所有用户输入的字符串字段做Trim处理的方法 https://www.kuaiidc.com/99517.html

相关文章

发表评论
暂无评论