Hi there
It's pretty easy to handing incoming JSON data in an action method, by using either JavaScriptSerializer or the slightly more awkward DataContractJsonSerializer. For example:
[ControllerAction]
public void ProcessJSON(string customerJsonData)
{
// Get a deserializer
System.Web.Script.Serialization.JavaScriptSerializer serializer =
new System.Web.Script.Serialization.JavaScriptSerializer();
// You could deserialize to a name-value pair, and access it as a weakly-typed object
IDictionary<string, object> myObject =
serializer.Deserialize<IDictionary<string, object>>(customerJsonData);
ViewData["name"] = myObject["Name"];
// Or, ideally, deserialize to a strongly-typed object
Customer myCustomer = serializer.Deserialize<Customer>(customerJsonData);
ViewData["name"] = myCustomer.Name;
RenderView("CustomerDetails");
}
If you are trying to receive the JSON data as a strongly-typed .NET object directly on your parameter list (e.g. changing the method signature above to
ProcessJSON(
Customer customerJsonData)), then no, unfortunately that won't work, you have to deserialize manually. If you're familiar with MonoRail's JsonBinder, you might like to check out
hammet's experiment to add MonoRail-style parameter binding to ASP.NET MVC.