I've noticed with ASP.NET MVC 2 that the model binder will not recognize "1" and "0" as true
and false
respectively. Is it possible to extend the model binder globally to recognize these and turn them into the appropriate boolean values?
Thanks!
Something among the lines should do the job:
public class BBinder : DefaultModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (value != null)
{
if (value.AttemptedValue == "1")
{
return true;
}
else if (value.AttemptedValue == "0")
{
return false;
}
}
return base.BindModel(controllerContext, bindingContext);
}
}
and register in Application_Start
:
ModelBinders.Binders.Add(typeof(bool), new BBinder());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With