Create a ModelBinder for KeyValuePairTKey, TValue

3

The default ModelBinder of ASP.NET MVC is not able to bind the data with variables of type KeyValuePair. This is due to the way it has to link the complex types: first it creates a new instance of the object through a constructor without arguments and then it establishes the values of the different properties. In this case, the Key and Value properties of the KeyValuePair object are read-only, so you can assign values to it and the object remains empty.

The solution would be to create a custom ModelBinder for this type, but how could you create a ModelBinder for a generic type such as KeyValuePair?

    
asked by Asier Villanueva 12.05.2016 в 12:52
source

3 answers

0

In the end I solved it like this:

I have created a ModelBinder for the KeyValuePair data types

answered by 13.05.2016 / 13:16
source
2

Searching on the subject you can analyze this question similar to what you raise

ASP MVC.NET - how to bind KeyValuePair?

there creates a custom ModelBinder where it detects the data type of the model and if it is a KeyValuePair it selects from the collection of values that come from the request and you assign it to an instance using Activator.CreateInstance

Analyze the example raised by Petr J

> > > wanted to look at the option to create a ModelBinderProvider that discriminates which ModelBinder to use for the KeyValuePair types

maybe you could evaluate

aspnetwebstack Binders

There you will see two classes: KeyValuePairModelBinder.cs and KeyValuePairModelBinderProvider.cs

I think you point to something like that

    
answered by 12.05.2016 в 16:12
-1

In MVC5 there is the KeyValuePairModelBinderProvider Class

Greetings

    
answered by 13.05.2016 в 12:29