- 'mailto:// email@email.com ' or
- href=' http://www.wildesoft.net '
So what I need to remember is to either a) trim before I send to the database or b) trim after I retrieve from the database. I for one prefer option one, better to keep the database correct rather than fixing output in the UI. But is there a way that we can do this for every html control that is posted? With asp.net MVC we can override the default model binder which performs a trim before the value gets to the action method on a controller.
All you need to do is to add the following class to your mvc project:-
public class TrimModelBinder : DefaultModelBinder { protected override void SetProperty( ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value) { if (propertyDescriptor.PropertyType == typeof(string)) { var val = (string)value; if (!string.IsNullOrEmpty(val)) val = stringValue.Trim(); value = val; } base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value); } }Then in your application start up method:-
protected void Application_Start() { InitContainer(); ... ModelBinders.Binders.DefaultBinder = new TrimModelBinder(); }Now when a user keys in a leading or trailing space into any input control then the TrimModel binder kicks in and automatically removes it for you.
The only instance where this may be a problem is when a user has a leading or trailing space on their password.
Nice job! It works very fine.
ReplyDeleteThanks a lot.
Excellent, glad it helps you out. Thanks for your comment
ReplyDeleteOn line 12, what is stringValue? Undefined variable.
ReplyDeleteSorry was a bit hasty with that comment. Simply changed line 12 to:
Deleteval = value.ToString().Trim();
and all is well.
Thanks for the article!
What about Password type string Property ? How we bifurcate with other string ?
ReplyDeletehi did you read my last point. "The only instance where this may be a problem is when a user has a leading or trailing space on their password."
ReplyDeleteI agree with you however this is helpful for ideal scenario where password field is not present but such application are rare or limited. So how we can take this to next level where password field can eliminate .....
Delete2 options:-
Delete1. live with it, when user enters password into registration or login space is ALWAYS removed hence user never need to know
2. move it so each model (class) has a data attribute that trims
It may be possible to have a data attribute on the password property that the trim ignores.
HTH but you point is outside this post
Thanks for sharing this, but I have a problem using this code: On SetProperty I have this message: "No suitable method found to override". Do you know how fix this?
ReplyDeleteThanks!
what about the case, when the property is list of strings
ReplyDelete