taskeenkayani / ext-direct-mvc

Automatically exported from code.google.com/p/ext-direct-mvc
0 stars 0 forks source link

Checkbox value is always false #8

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Hi,

first of all: thanks for this nice library, it works nearly perfekt ;)

The issue is, i have a checkbox in my FormPanel. At the submit, the form send 
the value "on" but at the serverside the corresponding field is always false. 
Is there a mapping between on = true and off = false?

To confirm my theory, I modified the example and added a checkbox. I also added 
the field to the Person POCO Class.

Is this a bug/missing feature or only my stupidity :)

Regards
Pascal

Original issue reported on code.google.com by m...@pascalwalter.de on 25 Jun 2010 at 12:56

GoogleCodeExporter commented 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

GoogleCodeExporter commented 8 years ago
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

GoogleCodeExporter commented 8 years ago
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