Imagine this Javascript dictionary:
var things = {};
things['1'] = 10;
things['2'] = 11;
Here's a little bit of ajax code:
$.ajax({
url: '/Controller/Foo',
type: 'POST',
data: {
things: things
},
Here's something that works:
[HttpPost]
public IActionResult Foo(Dictionary<string, int> things)
things will show that 1 maps to 10 and 2 maps to 11.
Here's something that DOES NOT work:
[HttpPost]
public IActionResult Foo(Dictionary<string, object> things)
things will show that 1 maps to null and 2 maps to null.
I can not change the Dictionary types. In reality, that Dictionary is part of a complex object that is used throughout the application (on the C# side of things). What you are looking at is a dumbed-down example.
In addition, JSON.stringify does not help at all. In fact, if I stringify the dictionary I get a count of 0, with no values.
Using C# to express my point, I think the expectation is more this (than what is currently happening):
int x = 5;
object foo = (object)x;
The dictionary was defined like this because one could be doing:
things[key] = 1;
or
things[key] = "string";
that's why it was declared as an object.
I am using ASP.NET Core if that matters (and jquery 3.4.1).
Thank you.