I'm using jQuery to make an Ajax call using an Http Post in ASP.NET MVC. I would like to be able to pass a Dictionary of values.
The closest thing I could think of was to pass in a multi-dimensional array of strings, but the result that actually gets passed to the ActionResult method is a single dimensional string array containing a string concatenation of the "key/value" pair.
For instance the first item in the below "values" array contains the below value:
"id,200"
Here's an example of my ActionResult method:
public ActionResult AddItems(string[] values)
{
// do something
}
Here's an example of how I'm calling the method from jQuery:
$.post("/Controller/AddItems",
{
values: [
["id", "200"],
["FirstName", "Chris"],
["DynamicItem1", "Some Value"],
["DynamicItem2", "Some Other Value"]
]
},
function(data) { },
"json");
Does anyone know how to pass a Dictionary object from jQuery to the ActionResult method instead of an Array?
I would really like to define my ActionResult like this:
public ActionResult AddItems(Dictionary<string, object> values)
{
// do something
}
Any suggestions?
UPDATE: I tried passing in a comma within the value and it basically just makes it impossible to actually parse the key/value pair using string parsing.
Pass this:
values: [
["id", "200,300"],
["FirstName", "Chris"]
]
results in this:
values[0] = "id,200,300";
values[1] = "FirstName,Chris";
See Question&Answers more detail:
os