c# - MVC custom model binder using the default binder for certain form values -


I have a custom model binder, which are in an action method to a particular parameter:

  public override ActionResult MyAction (integer someData, [ModelBinder (typeof (MyCustomModelBinder))] list of & lt; MyObject & gt; MyList ...)   

it is good Works like - Binder is said to be expected in the form of a machine. However, I want to invite the default model binders for certain addtional values ​​in the request. Form collection form key such name:

  dataFromView [0] .key dataFromView [0] .Value dataFromView [1] .key dataFromView [1] .Value   

The default model binder converts these values ​​into an IDictionary if I add an IDictionary as a parameter to the verb method.

However, I want to manipulate these values ​​at the model binder level (together with the original list above the object).

Is there a way to get this model to get the default model binder to form my value in the

BindModel () method of a model binder?
  public override object BindModel (controllerContext controllerContext, ModelBindingContext bindingContext) {// form must be the default model binder to provide IDictionary values ​​...}   

I've tried to use bindingContext.ValueProvider.GetValue but it seems to always return to the null when I'm trying to cast an IDictionary.

Here is a possible solution that I found (looking at the default model binder source code) Allows to use the default Model Binders functionality to create dictionaries, lists, etc.

A new recount of ModelBindingContext require binding values:

  var dictionaryBindingContext = new ModelBindingContext () {ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType (() = & gt; null, typeof (IDictionary & LT; long, integer & gt;)), ModelName = "dataFromView", // name (s) form elements you want to be in the dictionary of ModelState = bindingContext.ModelState, PropertyFilter = bindingContext. PropertyFilter, ValueProvider = BindingContext.ValueProvider}; Var boundValues ​​= base.BindModel (Controller Consultate, DictionaryBangingTontax);   

Now the default model binder is applied with the binding context you specify and the bound object will return normally.

So far it seems to work ...

Comments