當前位置:
首頁 > 知識 > Asp.net MVC 如何對所有用戶輸入的字元串欄位做Trim處理

Asp.net MVC 如何對所有用戶輸入的字元串欄位做Trim處理

經常需要對用戶輸入的數據在插入資料庫或者判斷之前做Trim處理,針對每個ViewModel的欄位各自做處理是我們一般的想法。最近調查發現其實也可以一次性實現的。

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處理

總結

就是這麼簡單,為以後做備忘.

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 達人科技 的精彩文章:

一道C語言安全編碼題目
並行模式庫PPL應用實戰(一):使用task類創建並行任務
Linux之rsync遠程同步
EF Core學習Code First
多線程和非同步編程示例和實踐-踩過的坑

TAG:達人科技 |

您可能感興趣

Okex關於V3 OpenAPI現貨/槓桿介面手續費欄位的優化
Excel-使用公式執行多欄位排序(SUMPRODUCT)
SpringData JPA 在解析實體類欄位時駝峰自動添加下劃線問題
django 自帶 user 欄位擴展及頭像上傳
轉 fastjson 過濾不需要的欄位或者只要某些欄位
MYSQL包含逗號的欄位拆分查詢
Excel小技巧!如何讓科學計數E+欄位恢復正常
果殼中的-SAP許可權欄位
鮮為人知的HTTP協議頭欄位詳解大全
MYSQL 資料庫名、表名、欄位名查詢
如何「爆破檢測」加密密碼欄位和存在驗證碼的Web系統
製造商代碼欄位在J1939中的位置你搞懂了嗎?
2月1日在線修正:賬號角色欄位增至16個
新功能-數字或日期類型的擴充欄位新增輸入助手功能
魔獸世界2月1日在線修正,角色欄位增加至16個