Is there a way to access the attributes of the controller action parameter currently being processed from within IModelBinder.BindModel()?
In particular, I am writing a binder for binding request data to an arbitrary Enum type (specified as a template parameter to the model binder) and I would like to specify for each controller action parameter for which I want to use this binder a name of the HTTP request value to get the Enum values from.
Example:
public ViewResult ListProjects([ParseFrom("jobListFilter")] JobListFilter filter)
{
...
}
and the model binder:
public class EnumBinder<T> : IModelBinder
{
public object BindModel(ControllerContext controllerContext,
ModelBindingContext bindingContext)
{
HttpRequestBase request = controllerContext.HttpContext.Request;
// Get the ParseFrom attribute of the action method parameter
// From the attribute, get the FORM field name to be parsed
//
string formField = GetFormFieldNameToBeParsed();
return ConvertToEnum<T>(ReadValue(formField));
}
}
I suspect there may possibly be another, more appropriate, point in the request workflow where I would supply the attribute value.
Found out how to do it using
CustomModelBinderAttribute-derived class:Now the action method looks like this: