Skip to content

Instantly share code, notes, and snippets.

@jincod
Created April 26, 2012 15:50
Show Gist options
  • Save jincod/2500521 to your computer and use it in GitHub Desktop.
Save jincod/2500521 to your computer and use it in GitHub Desktop.
Posting json object which will bind IDictionary<>
public class Notification
{
public string Name { get; set; }
public IDictionary<string, string> Fields { get; set; }
}
[HttpPost]
public ActionResult Send(string id, Notification viewModel)
{
string name = viewModel.Name;
IDictionary<string, string> fields = viewModel.Fields;
return Json("Success");
}
$.ajax({
type: 'POST',
url: "/home/send",
data: JSON.stringify({
id: "id1",
"Name": "name1",
"Fields": [{
"Key": "key1",
"Value": "value1"
}, {
"Key": "key2",
"Value": "value3"
}]
}),
contentType: "application/json",
dataType: "json",
processData: false
});
@alexbeletsky
Copy link

Cool, thanks a lot!

But, what I really wanted to archive is then you able to POST

{
id: 1,
name: "name",
fields: {
"key" : "value"
}
};

since it's native JSON representation of Javascript object.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment