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!