Background
Many times there is a need to work with JSON (JavaScript object Notation) in ASP.NET MVC with the help of jQuery Ajax call. So let's start step by step, so it will be useful to understand from scratch.
Step 1 : Create MVC application
Many times there is a need to work with JSON (JavaScript object Notation) in ASP.NET MVC with the help of jQuery Ajax call. So let's start step by step, so it will be useful to understand from scratch.
Step 1 : Create 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" and provide the Project a name as you wish and click on OK.
- Choose MVC empty application option and click OK
Right click on Model folder in the created MVC application, give the class name Employee or as you wish and click on 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 Home or as you wish and click OK.
HomeController.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.
To work with JQuery we need the following JQuery library,
To work with JQuery we need the following JQuery library,
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
Step 4 : Add Partial view
Right click on Home folder inside the View folder in the created MVC application as:
Give the name EmpDetails, click on Add button and write the following code.
- <script src="~/Scripts/jquery-1.10.2.min.js"></script>
- <script>
- $(document).ready(function () {
- //Call EmpDetails jsonResult Method
- $.getJSON("Home/EmpDetails",
- function (json) {
- var tr;
- //Append each row to html table
- for (var i = 0; i < json.length; i++) {
- tr = $('<tr/>');
- tr.append("<td>" + json[i].Id + "</td>");
- tr.append("<td>" + json[i].Name + "</td>");
- tr.append("<td>" + json[i].City + "</td>");
- tr.append("<td>" + json[i].Address + "</td>");
- $('table').append(tr);
- }
- });
- });
- </script>
- <table class="table table-bordered table-condensed table-hover table-striped">
- <thead>
- <tr>
- <th>Id</th>
- <th>Name</th>
- <th>City</th>
- <th>Address</th>
- </tr>
- </thead>
- <tbody></tbody>
- </table>
Now call the partial view EmpDetails in Main Index view as in the following code.
- @{
- ViewBag.Title = "www.compilemode.com";
- }
- <div style="margin-top:20px">
- @Html.Partial("EmpDetails");
- </div>
After running the application the output will be as in the following screenshot,
From the preceding examples we learned how to bind HTML table using JSON data in MVC.
Summary
I hope this tutorial is useful for all readers. If you have any suggestion then please comment below.

Chintan GoswamiPosted Aug 8, 2018, 7:56 AM
Public ActionResult EditState(int id) { StateModel sm = new StateModel(); var state = sm.EditState(id); if (state != null) { return Json(state, JsonRequestBehavior.AllowGet); } else { return null; } //return View(); }
Jayendra MistryPosted Apr 9, 2017, 1:51 PM
Plz help Me to diaply JSONResult Data in HTML Table
Jayendra MistryPosted Apr 9, 2017, 1:49 PM
I can't get output only display Json result data but can't display in HTML Table
Vithal WadjePosted Nov 30, 2015, 1:34 AM
Thanks
Santhakumar MunuswamyPosted Nov 29, 2015, 11:52 PM
Good one
Vithal WadjePosted Nov 29, 2015, 10:35 PM
Thanks Banketeshvar Narayan sir
Vithal WadjePosted Nov 29, 2015, 10:35 PM
Thanks Harshad Pansuriya sir
Banketeshvar NarayanPosted Nov 28, 2015, 3:18 AM
Good One
Harshad PansuriyaPosted Nov 27, 2015, 11:08 PM
Nice one
Vithal WadjePosted Nov 27, 2015, 3:53 PM
ace mary can you explain exact problem
Vithal WadjePosted Nov 27, 2015, 3:50 PM
thanks Aqib sir
Muhammad Aqib ShehzadPosted Nov 27, 2015, 5:11 AM
good one
Ace MaryPosted Nov 27, 2015, 2:43 AM
Just starting out with Kendo and JSON and not having much luck in binding a grid to a json datasource. Here's my data, stored as a file called 'data.json' stored in the same directory as the page:{ “firstName”: “John”, “lastName”:”Smith”, “department”:”Human Resources” } and my page: <%@ Page Language="vb" AutoEventWireup="false" CodeBehind="Kendo1.aspx.vb" Inherits="KendoUI.Kendo1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <link href="Styles/kendo.common.min.css" rel="stylesheet" type="text/css" /> <link href="Styles/kendo.default.min.css" rel="stylesheet" type="text/css" /> <!--Then paste the following for Kendo UI Web scripts--> <script src="js/jquery.min.js" type="text/javascript"></script> <script src="js/kendo.web.min.js" type="text/javascript"></script> </head> <body> <form id="form1" runat="server"> <script type="text/javascript"> $(document).ready(function () { var ds = new kendo.data.DataSource({ transport: { read: { url: "data.JSON", dataType: "json" } } }); $("#grid").kendoGrid({ dataSource: ds, columns: [ { field: "firstName", title: "First Name" }, { field: "lastName", title: "Last Name" }] }); }); </script> <div> <input id="myAutoComplete" /> </div> <!-- Define the HTML div that will hold the Grid --> <div id="grid" style="width: 40%; margin: auto;"> </div> </form> </body> </html> http://goo.gl/8aE6LK | http://goo.gl/LYLHhG