How To Get A Dynamically Created Json Data Set In Mvc 3 Controller?
Ok, so I am using MVC 3 and it is great at de-serializing a JSON data set into a strongly typed object that is passed to my controller action. Unfortunately I have not found a solu
Solution 1:
You Could Use JavaScriptSerializer or DataContractSerializer with Some ActionFilters. They're very flexible
Solution 2:
See my answer Passing dynamic json object to C# MVC controller basically using a dynamic type and a ValueProviderFactory is the cleanest way to deserialize Json to something more dynamic.
Solution 3:
There's another option which i prefer using for being neater...(we eliminate the step of getting data from the request stream)
Here's a code sample
CatcatObj=newCat();
if (TryUpdateModel<Cat>(catObj))
{
//do stuff
}
else
{
//invalid input
}
The TryUpdateModel resides in the controller namespace and hence no need to add any additional reference.
If you just need the Json sent in as part of the request you could obtain it using the following block of code(you could also obtain it from Request.Form
)
using (StreamReader reader = new StreamReader(Request.InputStream))
{
var inputJson = reader.ReadToEnd();
}
Post a Comment for "How To Get A Dynamically Created Json Data Set In Mvc 3 Controller?"