Created
October 22, 2012 14:34
-
-
Save johnnyreilly/3931778 to your computer and use it in GitHub Desktop.
Documenting a JsonValueProviderFactory Gotcha (MVC 3 meet Dictionary)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
[HttpPost] | |
public ActionResult PostDictionary(Dictionary<string, string> myDictionary) | |
{ | |
return Json(myDictionary); | |
} | |
//... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
[HttpPost] | |
public ActionResult PostDictionary(string myDictionary) | |
{ | |
var actualDictionary = new System.Web.Script.Serialization.JavaScriptSerializer() | |
.Deserialize<Dictionary<string, string>>(myDictionary); | |
return Json(actualDictionary); | |
} | |
//... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.ajax("PostDictionary", { | |
type: "POST", | |
contentType: "application/json", | |
data: JSON.stringify({ | |
myDictionary: { | |
"This": "is", | |
"a": "dictionary" | |
} | |
}), | |
success: function (result) { | |
alert(JSON.stringify(result)); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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