1

Assuming these classes\enums:

public enum Test
{
    MyTest1 = 0,
    MyTest2,
    MyTest3,
}

public class Model
{
    public Dictionary<Test, string> TestDictionary { get; set; }
    public Dictionary<string, string> StringDictionary { get; set; }
    public string Other { get; set; }

    public Model()
    {
        TestDictionary = new Dictionary<Test, string>();
        StringDictionary = new Dictionary<string, string>();
    }
}

This form:

<form action="/Home/Test" method="post">
    <input type="hidden" name="Model.TestDictionary[MyTest1]" value="myval" />
    <input type="hidden" name="Model.StringDictionary[mykey]" value="myval" />
    <input type="hidden" name="Model.StringDictionary[mykey2]" value="myval2" />
    <input type="hidden" name="Model.Other" value="works" />
    <button type="submit">Submit</button>
</form>

And this action:

public ActionResult Test(FormCollection formCollection)
{
    Model model = new Model();
    TryUpdateModel(model, "Model");
    return Redirect("Index");
}

The Model.StringDictionary get's updated successfully.
I can't get the Model.TestDictionary to get updated.

I've tried numerous ways, like changing the inputs name to Test.MyTest1 but couldn't get this to work.

How should my input look like id I want to populate the Model.TestDictionary ?

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • Check this topic http://stackoverflow.com/questions/6051756/model-binding-to-enums-in-asp-net-mvc-3 – kamil-mrzyglod May 19 '15 at 12:13
  • @Kamo - Thanks for your answer. This is not the issue here since the binder looks for the "first level" properties (In my case two dictionaries and a string). – Amir Popovich May 19 '15 at 12:35

1 Answers1

0

I found an ugly work-around that works:

<input type="hidden" name="Model.TestDictionary[0].Key" value="MyTest1" />
<input type="hidden" name="Model.TestDictionary[0].Value" value="myval" />

Anyway, If someone finds something better then it will be nice to see.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99