Bind HTML Table Using JSON Data In ASP.NET MVC

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
  1. "Start", then "All Programs" and select "Microsoft Visual Studio 2015".

  2. "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.

  3. Choose MVC empty application option and click OK
Step 2 : Create Model Class

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
  1. public class Employee  
  2. {  
  3.     public int Id { getset; }  
  4.     public string Name { getset; }  
  5.     public string City { getset; }  
  6.     public string  Address { getset; }  

Step 3 : Add controller class

Right click on Controller folder in the created MVC application, give the class name Home or as you wish and click OK.

HomeController.cs
  1. public class HomeController: Controller  
  2. {  
  3.     // GET: Home    
  4.     public ActionResult Index()  
  5.         {  
  6.             return View();  
  7.         }  
  8.         [HttpGet]  
  9.     public JsonResult EmpDetails()  
  10.     {  
  11.         //Creating List    
  12.         List < Employee > ObjEmp = new List < Employee > ()  
  13.         {  
  14.             //Adding records to list    
  15.             new Employee  
  16.             {  
  17.                 Id = 1, Name = "Vithal Wadje", City = "Latur", Address = "Kabansangvi"  
  18.             },  
  19.             new Employee  
  20.             {  
  21.                 Id = 2, Name = "Sudhir Wadje", City = "Mumbai", Address = "Kurla"  
  22.             }  
  23.         };  
  24.         //return list as Json    
  25.         return Json(ObjEmp, JsonRequestBehavior.AllowGet);  
  26.     }  
  27. }  
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,
  1. <script src="~/Scripts/jquery-1.10.2.min.js"></script> 
The preceding JQuery library file version may be different that is lower or higher.

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.
  1. <script src="~/Scripts/jquery-1.10.2.min.js"></script>  
  2. <script>  
  3.     $(document).ready(function () {  
  4.         //Call EmpDetails jsonResult Method  
  5.         $.getJSON("Home/EmpDetails",  
  6.         function (json) {  
  7.         var tr;  
  8.         //Append each row to html table  
  9.         for (var i = 0; i < json.length; i++) {  
  10.                 tr = $('<tr/>');  
  11.                 tr.append("<td>" + json[i].Id + "</td>");  
  12.                 tr.append("<td>" + json[i].Name + "</td>");  
  13.                 tr.append("<td>" + json[i].City + "</td>");  
  14.                 tr.append("<td>" + json[i].Address + "</td>");  
  15.                 $('table').append(tr);  
  16.             }  
  17.         });  
  18.     });  
  19. </script>  
  20. <table class="table table-bordered table-condensed table-hover table-striped">  
  21.         <thead>  
  22.         <tr>  
  23.         <th>Id</th>  
  24.         <th>Name</th>  
  25.         <th>City</th>  
  26.         <th>Address</th>  
  27.         </tr>  
  28.         </thead>  
  29.         <tbody></tbody>  
  30. </table> 
Step 5: Call partial view EmpDetails in Main Index view

Now call the partial view EmpDetails in Main Index view as in the following code.
  1. @{  
  2.     ViewBag.Title = "www.compilemode.com";  
  3. }  
  4. <div style="margin-top:20px">  
  5. @Html.Partial("EmpDetails");  
  6. </div> 
Step 6: Run the application

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.


Similar Articles