I'm trying to understand how custom binding should work.
Assuming a simple Action of
[HttpPost]
public ActionResult MyAction(CustomType parameter) {
// do something
}
... and the following form data
{
parameter : "mydata"
parameter.Property1 : "something"
parameter.Property2 : 3
}
... and the following, very simple custom binder
public class MyBinder : DefaultModelBinder {
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) {
if (bindingContext.ModelType.Equals(typeof(CustomType))) {
string parameter = controllerContext.HttpContext.Request.Form[bindingContext.ModelName];
object model = controllerContext.HttpContext.Cache[parameter];
return model;
}
return base.BindModel(controllerContext, bindingContext);
}
protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Type modelType) {
// not called
return base.CreateModel(controllerContext, bindingContext, modelType);
}
protected override void BindProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, System.ComponentModel.PropertyDescriptor propertyDescriptor) {
// not called
base.BindProperty(controllerContext, bindingContext, propertyDescriptor);
}
}
I can see that the BindModel
method is called. However, once I return my custom object the BindProperty
method is never called for Property1
and Property2
. This makes sense, because I'm not calling base.BindModel()
.
So my question is: How should BindModel
be implemented so that it creates CustomType
and also calls BindModel
in the super class?