views:

141

answers:

1

I'm in the process of creating my own custom ModelBinder that inherits from DefaultModelBinder, and manually binds XElement-typed properties.

Now it seems that I have to override the 'BindProperty' method, like so:

    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 be using to:

A) get the value of the property from the posted Form values?

B) inject this value into the property?

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

I need someone who's done this before to walk me through it.

+2  A: 

The bindingContext parameter contains a ValueProvider property that is already populated with the values from the request. The idea is that you pull the values from that.

It is simply a dictionary of values, so you can index into it using the name of the field you would like to bind.

The easiest way to understand what's goint on is to apply your custom ModelBinder and then set a breakpoint in your code and inspect what data you got while in the debugger.

Mark Seemann