MVC custom model binder using the default binder for certain form values

asp.net-mvc, c#, model-binding

Solution

Here's a potential solution I found (by looking at the default model binder source code) which allows you to use the default model binders functionality for creating a Dictionary, List etc.

Create a new ModelBindingContext detailing the binding values you require:

var dictionaryBindingContext = new ModelBindingContext()
            {
                ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(() => null, typeof(IDictionary<long, int>)),
                ModelName = "dataFromView", //The name(s) of the form elements you want going into the dictionary
                ModelState = bindingContext.ModelState,
                PropertyFilter = bindingContext.PropertyFilter,
                ValueProvider = bindingContext.ValueProvider
            };

var boundValues = base.BindModel(controllerContext, dictionaryBindingContext);

Now the default model binder is invoked with the binding context you have specified and will return the bound object as normal.

Seems to work so far...

Problem

I have a custom model binder which is invoked for a particular parameter going into an action method: ``` public override ActionResult MyAction(int someData, [ModelBinder(typeof(MyCustomModelBinder))]List<MyObject> myList ... ) ``` This works well - the binder is called as expected. However, I want to invoke the default model binder for some addtional values that are in the Request.Form collection. The form keys are named like this: ``` dataFromView[0].Key dataFromView[0].Value dataFromView[1].Key dataFromView[1].Value ``` The default model binder nicely converts these values into an IDictionary if I add an IDictionary as a parameter on the action method. However, I want to manipulate these values at the model binder level (along with the original List object above). Is there a way to get the default model binder to create this dictionary from the form values for my in the `BindModel()` method of my custom model binder? ``` public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { //Get the default model binder to provide the IDictionary from the form values... } ``` I've tried to using the bindingContext.ValueProvider.GetValue but it always seems to return null when I'm trying to cast to an IDictionary.

Original source