Open GoogleCodeExporter opened 8 years ago
JSON.Net is not parsing the value "on" as a boolean true value.. I've added a
feature request to json.net to support this 'true' value. For now, we should
create a custom convertor.
Original comment by marco.di...@gmail.com
on 1 Jul 2010 at 2:15
Other option is this:
public class ChekboxModelBinder : DefaultModelBinder
{
protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder) {
if (propertyDescriptor.PropertyType == typeof(bool) || propertyDescriptor.PropertyType == typeof(bool?)) {
string value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
if (value == "on")
return true;
}
return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
}
}
ModelBinders.Binders.DefaultBinder = new ChekboxModelBinder();
Original comment by marco.di...@gmail.com
on 2 Jul 2010 at 9:26
Thank you this really helps.
I found another problem I quess: Parsing decimals
ExtJS is english but my Developing Machine is german, so my
Thread.CurrentCulture is German... Problem is, that ExtJS sends 1.34 but the
Serverside doesnt parse this.
I dont know if this is a bug in JSON.NET but it isn't nice...
So I extended your Idea to:
public class ExtDirectModelBinder : DefaultModelBinder
{
protected override object GetPropertyValue(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor, IModelBinder propertyBinder)
{
if (propertyDescriptor.PropertyType == typeof(bool) || propertyDescriptor.PropertyType == typeof(bool?))
{
string value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
if (value == "on")
return true;
}
else if (propertyDescriptor.PropertyType == typeof(decimal) || propertyDescriptor.PropertyType == typeof(decimal?))
{
string value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;
return Convert.ToDecimal(value.Replace(".", ","));
}
return base.GetPropertyValue(controllerContext, bindingContext, propertyDescriptor, propertyBinder);
}
}
Original comment by m...@pascalwalter.de
on 6 Jul 2010 at 3:14
Original issue reported on code.google.com by
m...@pascalwalter.de
on 25 Jun 2010 at 12:56