views:

35

answers:

1

I understand that a ModelBinder is a good place to do work on the request so that you keep that type of code out of the controller. Working with Form Values would be an example. That seems to makes sense however, I inherited an application that is using a Custom Binder and I just can't seem to figure out how or why it works.

The binder itself exists to deal with only TimeZoneInfo objects as they (Time Zones) are used within the application so it is registered in the Application_Start method in the global like so:

  binders.Add(new System.Collections.Generic.KeyValuePair<Type, IModelBinder>(typeof(TimeZoneInfo), new TimeZoneInfoModelBinder()));

Where binders is of type ModelBinderDictionary. The binder itself then looks like this:

public class TimeZoneInfoModelBinder : DefaultModelBinder
{
    public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        if (bindingContext == null)
        {
            throw new ArgumentNullException("bindingContext");
        }
        string tzId = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue;


        try
        {
            return TimeZoneInfo.FindSystemTimeZoneById(tzId);
        }
        catch (Exception ex)
        {
            bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex);
        }
        return null;
    }
}

Now from stepping through the code I know this Binder is only called when I POST data back to the server that involves a particular Model which has a TimeZoneInfo property on it. My assumptions are:

  • The runtime "knows" about all of the models and only invokes the custom binder when it finds a property with the same type as the one that was passed in to the binder.
  • The reason this is being done is because the TimeZoneInfo type is a complex type and, therefore, can't be implicitly converted to from a string (POST data)

Is this a correct understanding of this particular instance or am I missing something?

Thanks!

+1  A: 

You're close...

The model binder considers all of the data from the request's form data, the URL query string, and routing parameters. The binder will attempt to bind to the arguments of the action method and to any public properties of the model that have both a get and set defined. This is done by considering the name of the parameter and/or public property.

Since all of the input sources are in a name-value format, the names are matched. For example, if there is an "id" routing parameter and your action method has an "id" argument in its signature, the binder will bind the routing parameter to that action method argument. If there is a country form field and a model with a public country property, the model binder will attempt to bind the form field to the model. Etc.

The binder considers the type of the property when binding. The default model binder knows how to bind to the scalar properties (int, decimal, DateTime, etc.). It also can bind to a list (key format MyList[0]) or to a complex type that in turn exposes child properties (e.g, key format Rectangle.Width).

The example you have posted adds the ability to bind to a TimeZoneInfo object. That binder is being invoked because the name of a form parameter, query argument or routing parameter matches the name of a public property on your model that exposes a getter and setter that returns a TimeZoneInfo object.

Note the ModelBinder works in concert with the ModelState dictionary. It's important that invalid user input gets preserved even if it cannot be converted to the target type. In that case, an error is added to the ModelState dictionary and the raw value of the input parameter is kept so that it can be included in a re-rendering of the form with validation errors.

Rob
Thanks for the explanation Rob.
abszero