ASP.NET MVC 3 includes built-in JSON binding support that
enables action methods to receive JSON-encoded data and model-bind it to action
method parameters.
To see this feature in action, consider the jQuery
client-side JavaScript below. It defines a “save” event handler that will
be invoked when a save button is clicked on the client. The code within
the event handler constructs a client-side JavaScript “product” object with
three fields whose values are retrieved from HTML input elements. It then
uses jQuery’s .ajax() method to POST a JSON based request containing the
product to a /Store/UpdateProduct URL on the server:
ASP.NET MVC 3 now enables you to implement the
/Store/UpdateProduct URL on the server using an action method like below:
The UpdateProduct() action method above accepts a
strongly-typed Product object as a parameter. ASP.NET MVC 3 can now
automatically bind the incoming JSON post values to the .NET Product type on
the server – without you having to write any custom binding or marshalling
logic. ASP.NET MVC’s built-in model and input validation features all
work as you’d expect with this.
We think this capability will be particularly useful going
forward with scenarios involving client templates and data binding (like I’ve previously
blogged about here). Client templates will enable you to format and
display a single data item or set of data items by using templates that execute
on the client. ASP.NET MVC 3 will enable you to easily connect client
templates with action methods on the server that return and receive JSON data.