I have created a custom Model Binder that inherits from DefaultModelBinder, and overriding the BindProperty method to set the “duration” property to 35.
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor)
{
if (propertyDescriptor.Name == "Duration")
{
var request = controllerContext.HttpContext.Request;
var prefix = propertyDescriptor.Name;
SetProperty(controllerContext, bindingContext, propertyDescriptor, 35);
}
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
A breakpoint in this method gets hit, and all seems well, But when my action is hit after, my model has reverted back to giving Duration is default value of 0.
[HttpPost]
public ActionResult Create(ModelType model)
{ var val = model.Duration;
//val = 0 :(
}
I’m a little stuck, please help.
That’s because you are calling the base method. Try like this: