Returning JsonResult in ASP.NET MVC

Along with the ActionResult type (which is probably the most used), there is another type of return i.e. the JsonResult.

JsonResult returns objects in the JSON format. A single object will be returned as a single JavaScript object whereas an array or list of objects will be returned as a array to JavaScript.

So how does it work? I’ll be showing you a demo here.

Consider a jQuery AJAX call requesting some JSON data:

$.ajax({

  // The url to which the request is to be sent.
  url: "http://localhost/myproject/home/givejson",

  // The type of request. It can be "GET" or "POST" or others.
  type: "GET",

  // The data that is to be sent to the server. It can be omitted if no data is
  // to be sent. In a "GET" request this data is appended to the url.
  data: { "id": "foo", "name": "bar" },

  // The function to be called when the server returns some data. The data
  // variable holds the data that was returned.
  success: function (data) {
    document.write("Your id is " + data.id);
    document.write("Your name is " + data.name);
  },

  // Show an alert if an error occurs.
  error: function () {
    alert("Some error occured.");
  }
});

The above code will fire a request. Now let’s handle it in ASP.NET MVC. Consider a project named “myproject” containing a controller “homecontroller” and a method in the controller named “givejson” (hence the url):

Imports System.Web.Mvc

Public Class HomeController
  Inherits Controller

  ' The sent data automatically maps to the function's arguments. Only the names
  ' must be same.
  Function givejson(id As String, name As String) As JsonResult
    Dim obj As New myObj
    obj.id = id
    obj.name = name

    Return Json(obj, JsonRequestBehavior.AllowGet)
    ' The second argument is required if the request is a GET request. For POST
    ' requests omit the second argument.
  End Function
End Class

Public Class myObj
  Public Property id As String
  Public Property name As String
End Class

The above would handle the request and return the object to JavaScript which then writes it to document. If you are returning an array, then in the JavaScript function you can iterate over the data variable in a for loop.

If you are using POST request don’t forget to annotate the ASP.NET function with , like this:

...
  <HttpPost>
  Function givejson(id As String, name As String) As JsonResult
    Dim obj As New myObj
...

That’s it. If have any doubts relating to this, ask me in the comments below.

Advertisements

3 thoughts on “Returning JsonResult in ASP.NET MVC

Your Thoughts -

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s