MVC controller returns many types of output to the view according to the data we need for the application. In this article we will learn about JsonResult type of MVC . So instead of going into the depth on the subject, let us start with its practical implementation.
To know more about the Action result types please refer my previous article
What is ActionResult ?
ActionResult is the abstract class for showing the output to the client in various formats as in the result view returned by the controller. The ActionResult is defined in the controller and the controller returns to the client (the browser).
ActionResult is the abstract class for showing the output to the client in various formats as in the result view returned by the controller. The ActionResult is defined in the controller and the controller returns to the client (the browser).
What is JsonResult ?
JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format).
In this article we will learn about JsonResult by taking scenario to bind view using the JSON Data . So let's see step by step.
JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format).
In this article we will learn about JsonResult by taking scenario to bind view using the JSON Data . So let's see step by step.
Step 1 : Create an MVC application
- "Start", then "All Programs" and select "Microsoft Visual Studio 2015".
- "File", then "New" and click "Project" then select "ASP.NET Web Application Template", then provide the Project a name as you wish and click on OK.
- Choose MVC empty application option and click on OK
Right click on Model folder in the created MVC application, give the class name employee or as you wish and click OK.
Employee.cs
- public class Employee
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string City { get; set; }
- public string Address { get; set; }
- }
Right click on Controller folder in the created MVC application; give the class name. I have given class name Home and clicked on OK.
HomeControlle.cs
- public class HomeController : Controller
- {
- // GET: Home
- public ActionResult Index()
- {
- return View();
- }
- [HttpGet]
- public JsonResult EmpDetails()
- {
- //Creating List
- List<Employee> ObjEmp = new List<Employee>()
- {
- //Adding records to list
- new Employee {Id=1,Name="Vithal Wadje",City="Latur",Address="Kabansangvi" },
- new Employee {Id=2,Name="Sudhir Wadje",City="Mumbai",Address="Kurla" }
- };
- //return list as Json
- return Json(ObjEmp, JsonRequestBehavior.AllowGet);
- }
- }
In the above controller class JsonResult method EmpDetails we have added the records into the Generic list and returning it as JSON to avoid database query for same result.
Possible Error
If you return JSON method without setting JsonRequestBehavior property to AllowGet then the following error will occur.
Possible Error
If you return JSON method without setting JsonRequestBehavior property to AllowGet then the following error will occur.

Why error occurred ?
The GET request by default not allowed in JSON result so to allow GET request in JsonResult we need to set JsonRequestBehavior to AllowGet as in the following code snippet:
- Json(ObjEmp, JsonRequestBehavior.AllowGet);

Vithal WadjePosted Oct 18, 2015, 5:27 AM
Thanks sir
Santhakumar MunuswamyPosted Oct 18, 2015, 4:38 AM
Good one
Vithal WadjePosted Oct 9, 2015, 7:31 AM
Thanks
Humayun Kabir MamunPosted Oct 9, 2015, 7:17 AM
Nice...
Vithal WadjePosted Oct 9, 2015, 6:39 AM
Thanks
Sibeesh VenuPosted Oct 9, 2015, 5:17 AM
Nice Share
Vithal WadjePosted Oct 9, 2015, 4:22 AM
Thanks
Mukesh KumarPosted Oct 9, 2015, 3:31 AM
Nice share sir
Vithal WadjePosted Oct 9, 2015, 2:48 AM
Thanks
Harshad PansuriyaPosted Oct 9, 2015, 2:44 AM
Nice one
Vithal WadjePosted Oct 9, 2015, 2:40 AM
Thanks Akash sir
Vithal WadjePosted Oct 9, 2015, 2:39 AM
Thanks Nilesh Jadav sir
Akash VarshneyPosted Oct 9, 2015, 2:31 AM
Good Article !!
Nilesh JadavPosted Oct 9, 2015, 2:29 AM
Nice one sir