I have created the following simplified scenario to best describe my question. I have these two C# classes:
public class ClassOne
{
public Dictionary<string, Guid> Dict { get; set; }
}
public class ClassTwo
{
public Dictionary<Guid, string> Dict { get; set; }
}
I also have the following two actions:
public ActionResult ActionOne(ClassOne model)
{
// do some work here...
return RedirectToAction("Index");
}
public ActionResult ActionTwo(ClassTwo model)
{
// do some work here...
return RedirectToAction("Index");
}
And finally the following JavaScript:
var MyObject = {}
MyObject.Dict = {}
MyObject.Dict["c5a9f7a4-312a-45bd-9fc6-1e41fcd89764"] = "c5a9f7a4-312a-45bd-9fc6-1e41fcd89764";
MyObject.Dict["dc992a24-5613-4381-b199-7d4ebadb0635"] = "dc992a24-5613-4381-b199-7d4ebadb0635";
MyObject.Dict["c01d8501-e121-4b2d-80c5-8305bcec7aff"] = "c01d8501-e121-4b2d-80c5-8305bcec7aff";
$.ajax({
url: $(this).attr("action"),
type: "POST",
data: JSON.stringify(MyObject),
contentType: "application/json; charset=utf-8",
beforeSend: function () {
// do nothing
},
success: function (data) {
// success
},
error: function (jqXHR, textStatus, errorThrown) {
console.log(textStatus, errorThrown);
}
});
I have left out the JavaScript that routes the Ajax call to either ActionOne or ActionTwo to keep it simple. When the Ajax call is routed to ActionOne I get the following:
Which is what I expect. However when routing the Ajax call to ActionTwo (and therefore am expecting an instance of ClassTwo to have been created with it's Dictionary<Guid, string>
) I get the following:
Which is not what I am expecting. Am I missing something fundamental here? Why is the dictionary failing to be populated? Any guidance would be greatly appreciated. Thanks.