How can I modify input before model binding in asp.net mvc?
- by David G
How can I intercept submitted form input and modify it before it is bound to my model? For example, if I wanted to trim the whitespace from all text.
I have tried creating a custom model binder like so:
public class CustomBinder : DefaultModelBinder {
protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value) {
string newValue = ((string)value).Trim(); //example code to create new value but could be anything
base.SetProperty(controllerContext, bindingContext, propertyDescriptor, newValue);
}
}
but this doesn't seem to be invoked. Is there a better place to modify the input value?
Note: I need to modify the value before it is bound and validated.