I found all value passed by Model is not trimmed in ASP.net MVC3
Is there a way to:
- Apply a trim() on every field in Model (all string fields, at least; but all form fields are string before processed by Model, so better trim them all)
- Must before
ModelState.IsValid()(because I often found code stucked at weirdModelState.IsValidand later found because the form item did not be trimmed.)
Thanks.
You’ll have to create a custom model binder to trim any model property that is a string.
References:
Custom model binding using IModelBinder in ASP.NET MVC
Iterating on an ASP.NET MVC Model Binder
6 Tips for ASP.NET MVC Model Binding
A Better Model Binder
Basically, you can take one of two approaches:
IModelBinderinterfaceDefaultModelBinderclassExample