Sunday, 3 October 2010

Rendering Json in MVC

To render Json into an MVC view, you will need to use System.Web.Script.Serialization.JavaScriptSerializer which MVC uses internally when you call JsonResult.

Ideally what we would like to do is render a JSON object onto the page and then be able to refer to it through our script. For this we must attach it to the window object:
Then in our views we can then render a specific object that was passed to it from the controller: Then in our Javascript, we can then refer to the object using the window[object-name] property:

0 comments: