'MVC JSON Method not binding Dictionary properly

We are attempting to post data from JavaScript corresponds to a Dictionary<string, List> in C#

public class SubmittedObject {
    public string T;
    public SomeViewModel ViewModel;
}

public class SomeViewModel {
    public int Id;
    public Dictionary<string, List<ItemViewModel>> Items;
    public object Other;
}

public class ItemViewModel { 
    public int id; 
    public string value; 
} //shown as member variables for simplicity

JSON looks like:

{
    "t": "a",
    "viewModel": {
        "id": 0,
        "items": {
            "firstItem": [
                {
                    "id": 1,
                    "value": "1"
                }
            ],
            "secondItem": [
                {
                    "id": 2,
                    "value": "2"
                }
            ]
        },
        "other": null
    }
}

When the JS object gets submitted to

[HttpPost]
public ActionResult Save(string t, SubmittedObject viewModel)
{...}

saveViewModel gets interpreted with the first item in the Dictionary as key="FirstItem[0]" and value of null. SecondValue gets interpreted in like manner with key="SecondItem[0]" and value of null.

The current work-around is to serialize the submission data and submit it as a string; NewtonSoft parses it as expected.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source