Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

JsonResult Class

 

Represents a class that is used to send JSON-formatted content to the response.

Namespace:   System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)


type JsonResult = 
    class
        inherit ActionResult
    end

NameDescription
System_CAPS_pubmethodJsonResult()

Initializes a new instance of the JsonResult class.

NameDescription
System_CAPS_pubpropertyContentEncoding

Gets or sets the content encoding.

System_CAPS_pubpropertyContentType

Gets or sets the type of the content.

System_CAPS_pubpropertyData

Gets or sets the data.

System_CAPS_pubpropertyJsonRequestBehavior

Gets or sets a value that indicates whether HTTP GET requests from the client are allowed.

System_CAPS_pubpropertyMaxJsonLength

Gets or sets the maximum length of data.

System_CAPS_pubpropertyRecursionLimit

Gets or sets the recursion limit.

NameDescription
System_CAPS_pubmethodEquals(Object)

(Inherited from Object.)

System_CAPS_pubmethodExecuteResult(ControllerContext)

Enables processing of the result of an action method by a custom type that inherits from the ActionResult class.(Overrides ActionResult.ExecuteResult(ControllerContext).)

System_CAPS_protmethodFinalize()

(Inherited from Object.)

System_CAPS_pubmethodGetHashCode()

(Inherited from Object.)

System_CAPS_pubmethodGetType()

(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone()

(Inherited from Object.)

System_CAPS_pubmethodToString()

(Inherited from Object.)

You can return an instance of the JsonResult class by using the Json method.

The following example shows how to return an instance of the JsonResult class from an action method. The object that is returned specifies that a GET request is permitted.

public ActionResult Movies()
{
    var movies = new List<object>();

    movies.Add(new { Title = "Ghostbusters", Genre = "Comedy", Year = 1984 });
    movies.Add(new { Title = "Gone with Wind", Genre = "Drama", Year = 1939 });
    movies.Add(new { Title = "Star Wars", Genre = "Science Fiction", Year = 1977 });

    return Json(movies, JsonRequestBehavior.AllowGet);
}

The next example shows how to retrieve and display the JSON-formatted content.

<input name="btnGetMovies" id="btnGetMovies" type="submit" value="Get Movies">
<ul id="movieList"></ul>

<script src="~/Scripts/jquery-1.10.2.js"></script>
<script type="text/javascript">
    $("#btnGetMovies").click(function () {
        var actionUrl = '@Url.Action("Movies", "Home")';
        $.getJSON(actionUrl, displayData);
    });

    function displayData(response) {
        if (response != null) {
            for (var i = 0; i < response.length; i++) {
                $("#movieList").append("<li>" + response[i].Title + " " + response[i].Genre + " " + response[i].Year + "</li>")
            }
        }
    }
</script>

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show:
© 2015 Microsoft