JsonResult Class
Represents a class that is used to send JSON-formatted content to the response.
Assembly: System.Web.Mvc (in System.Web.Mvc.dll)
| Name | Description | |
|---|---|---|
![]() | JsonResult() | Initializes a new instance of the JsonResult class. |
| Name | Description | |
|---|---|---|
![]() | ContentEncoding | Gets or sets the content encoding. |
![]() | ContentType | Gets or sets the type of the content. |
![]() | Data | Gets or sets the data. |
![]() | JsonRequestBehavior | Gets or sets a value that indicates whether HTTP GET requests from the client are allowed. |
![]() | MaxJsonLength | Gets or sets the maximum length of data. |
![]() | RecursionLimit | Gets or sets the recursion limit. |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | (Inherited from Object.) |
![]() | ExecuteResult(ControllerContext^) | Enables processing of the result of an action method by a custom type that inherits from the ActionResult class.(Overrides ActionResult::ExecuteResult(ControllerContext^).) |
![]() | Finalize() | (Inherited from Object.) |
![]() | GetHashCode() | (Inherited from Object.) |
![]() | GetType() | (Inherited from Object.) |
![]() | MemberwiseClone() | (Inherited from Object.) |
![]() | ToString() | (Inherited from Object.) |
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.


