I am trying to implement a remote REST service which is used to handle all logic for my MVC3 web application, and so far I am able to retrieve the serialized object from the webservice, but I am stuck on deserializing the object into my ViewModel to pass to the View.
Here is my controller:
[HttpGet]
public ActionResult Index()
{
string versions;
using (var webClient = new WebClient())
{
versions = webClient.DownloadString("http://myservice/GetVersions");
}
// deserialize JSON/XML somehow...
//IEnumerable<VersionViewModel> model = ?
return View(model);
}
What do I need to do to convert the JSON I recieve from the webservice to a ViewModel to render my view? Thanks.
You could use RestSharp for the initial request, which should be able to automatically convert the JSON to a suitable data transfer object (DTO). From there, you could use something like AutoMapper to convert from DTO -> ViewModel class.
The DTO (without knowing what your JSON looks like, of course):
The final result something like:
The RestSharp wiki has lots of docs on how it maps the JSON onto your DTO classes, letting you worry less about serialization, and more about your business logic.