Skip to content

Instantly share code, notes, and snippets.

@johnnyreilly
Created October 22, 2012 14:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnnyreilly/3931778 to your computer and use it in GitHub Desktop.
Save johnnyreilly/3931778 to your computer and use it in GitHub Desktop.
Documenting a JsonValueProviderFactory Gotcha (MVC 3 meet Dictionary)
//...
[HttpPost]
public ActionResult PostDictionary(Dictionary<string, string> myDictionary)
{
return Json(myDictionary);
}
//...
//...
[HttpPost]
public ActionResult PostDictionary(string myDictionary)
{
var actualDictionary = new System.Web.Script.Serialization.JavaScriptSerializer()
.Deserialize<Dictionary<string, string>>(myDictionary);
return Json(actualDictionary);
}
//...
$.ajax("PostDictionary", {
type: "POST",
contentType: "application/json",
data: JSON.stringify({
myDictionary: {
"This": "is",
"a": "dictionary"
}
}),
success: function (result) {
alert(JSON.stringify(result));
}
});
$.ajax("PostDictionary", {
type: "POST",
contentType: "application/json",
data: JSON.stringify({
myDictionary: JSON.stringify({ //Note the deliberate double JSON.stringify
"This": "is",
"a": "dictionary"
})
}),
success: function (result) {
alert(JSON.stringify(result));
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment