Performing custom binding for a specific data type in ModelBinder

I am creating my own custom ModelBinder that inherits from DefaultModelBinder and manually binds the properties typed for the XElement.

Now it seems that I need to override the "BindProperty" method, for example:

    protected override void BindProperty(
        ControllerContext controllerContext, 
        ModelBindingContext bindingContext, 
        System.ComponentModel.PropertyDescriptor propertyDescriptor)
    {
        if (propertyDescriptor.PropertyType == typeof(XElement))
        {
            // code here to take the POST-ed form value and put it in the property as an XElement instance
        }
        else
            base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
    }

      

What code should I use for:

A) get property value from posted form values?

B) enter this value into a property?

I tried to run Reflector on the DefaultModelBinder class to see how it does it, but the code was very confusing.

I need someone who has done this before to get through it.

+2


source to share


1 answer


The bindingContext parameter contains a ValueProvider property that is already filled with values ​​from the request. The idea is that you extract values ​​from it.

It's just a dictionary of values, so you can index it using the name of the field you want to associate.



The easiest way to understand what goint on is is to apply your custom ModelBinder and then set a breakpoint in your code and check what kind of data you got in the debugger.

+3


source







All Articles