This project is read-only.

Improved model binder for dictionaries

Apr 3, 2012 at 12:48 PM
Edited Apr 3, 2012 at 12:49 PM

A while back I wrote an extension/replacement to the DefaultModelBinder that more easily allows dictionaries to be bound. It's basically an extension to the existing concept of array bindings, except with support to the generic dictionary. For example, if the post data is this:


capitals[Australia]=Canberra&capitals[USA]=Washington DC


and the action method is:


public ActionResult SaveCapitals(Dictionary<string, string> capitals)


You will get the dictionary "capitals" with


{ "Australia", "Canberra" },
{ "USA", "Washington DC" }

It works with complex bindings as well (i.e. objects with dictionaries/dictionaries of objects)

This is more intuitive than the current implementation where you specify separately key and value fields i.e.




More detailed explanation here:

The full source code is here:

I would love to adapt it for inclusion in the official MVC tree. Let me know what you guys think.