In this article I am going to show how to display data using jQuery, AJAX Call, JSON in ASP.NET MVC Application.

Open Visual Studio, then Add New Project.

New

mvc

Below is my Data Table in design mode from which I will show data.

data

Script of my Data Table,
  1. CREATE TABLE [dbo].[Emp_Information](
  2. [EMP_ID] [int] IDENTITY(1,1)NOTNULL,
  3. [Name] [varchar](50)NULL,
  4. [ProjectName] [varchar](50)NULL,
  5. [ManagerName] [varchar](50)NULL,
  6. [City] [varchar](50)NULL,
  7. [Joining_Date] [datetime] NULL,
  8. CONSTRAINT [PK_Emp_Information] PRIMARYKEYCLUSTERED
  9. (
  10. [EMP_ID] ASC
  11. )WITH (PAD_INDEX=OFF,STATISTICS_NORECOMPUTE=OFF,
  12. IGNORE_DUP_KEY=OFF,ALLOW_ROW_LOCKS=ON,ALLOW_PAGE_LOCKS=ON)ON [PRIMARY]
  13. )ON [PRIMARY]
  14. GO
  15. SETANSI_PADDINGOFF
  16. GO
Data in my Data Table:

Table

Now add an ADO.NET Entity Data Model. So, right click on Project Solution Explorer, Add, then click New Item,

new

Select ADO.NET Entity Data Model,

ef

efd

efd

efd

efd

efd

Now Right Click on Models Folder, Add, then New Class. Here's the screenshot,

add

Now add a new Controller, Right Click on Controller Folder and then Add New Controller,

Controller

controller

controller

Here in this controller do the following code:

controller
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using ShowListData_jQuery_JSON_MVC.Models;
  7. namespace ShowListData_jQuery_JSON_MVC.Controllers
  8. {
  9. public class EmployeeController: Controller
  10. {
  11. // GET: Employee
  12. public ActionResult Index()
  13. {
  14. return View();
  15. }
  16. public JsonResultGetAllEmployee()
  17. {
  18. CompanyDBEntitiesobj = new CompanyDBEntities();
  19. var contacts = obj.Emp_Information.Select(x => new
  20. {
  21. Id = x.EMP_ID,
  22. Name = x.Name,
  23. ProjectName = x.ProjectName,
  24. ManagerName = x.ManagerName,
  25. city = x.City
  26. }).ToList();
  27. return Json(contacts, JsonRequestBehavior.AllowGet);
  28. }
  29. }
  30. }
  31. Now my View(Index.cshtml):
  32. @ {
  33. ViewBag.Title = " Showing Data Using jQuery Ajax Call JSON in ASP.NET MVC";
  34. } <
  35. h1 >
  36. Showing Data Using jQuery Ajax Call JSON in ASP.NET MVC <
  37. /h1> <
  38. div >
  39. <
  40. tableid = "tblEmployee"
  41. class = "tblEmployee" >
  42. <
  43. thead >
  44. <
  45. img src = "~/Loading.gif"
  46. alt = "Loading"
  47. id = "imgLoading"
  48. class = "Load" / >
  49. <
  50. /thead> <
  51. tbody > < /tbody> <
  52. /table> <
  53. /div> <
  54. script src = "~/Scripts/jquery-1.10.2.min.js" > < /script> <
  55. script type = "text/javascript" >
  56. $(document).ready(function()
  57. {
  58. $("#tblEmployeetbodytr").remove();
  59. $.ajax
  60. ({
  61. type: 'POST',
  62. url: '@Url.Action("GetAllEmployee")',
  63. dataType: 'json',
  64. data: {},
  65. success: function(data)
  66. {
  67. $("#imgLoading").hide();
  68. var items = '';
  69. var rows = "<tr>" +
  70. "<th align='left' class='EmployeeTableTH'>Employee ID</th><th align='left' class='EmployeeTableTH'>Name</th><th align='left' class='EmployeeTableTH'>Project Name</th><th align='left' class='EmployeeTableTH'>Manager Name</th><th align='left' class='EmployeeTableTH'>City</th>" +
  71. "</tr>";
  72. $('#tblEmployeetbody').append(rows);
  73. $.each(data, function(i, item)
  74. {
  75. var rows = "<tr>" +
  76. "<td class='EmployeeTableTD'>" + item.Id + "</td>" +
  77. "<td class='EmployeeTableTD'>" + item.Name + "</td>" +
  78. "<td class='EmployeeTableTD'>" + item.ProjectName + "</td>" +
  79. "<td class='EmployeeTableTD'>" + item.ManagerName + "</td>" +
  80. "<td class='EmployeeTableTD'>" + item.city + "</td>" +
  81. "</tr>";
  82. $('#tblEmployeetbody').append(rows);
  83. });
  84. },
  85. error: function(ex)
  86. {
  87. var r = jQuery.parseJSON(response.responseText);
  88. alert("Message: " + r.Message);
  89. }
  90. });
  91. return false;
  92. }); <
  93. /script> <
  94. styletype = "text/css" >
  95. .tblEmployee
  96. {
  97. font - family: verdana, arial, sans - serif;
  98. font - size: 11 px;
  99. color: #333333;
  100. border-width: 1px;
  101. border-color: # 666666;
  102. border - collapse: collapse;
  103. }
  104. .EmployeeTableTH
  105. {
  106. border - width: 1 px;
  107. padding: 8 px;
  108. border - style: solid;
  109. border - color: #666666;
  110. background-color: # dedede;
  111. }
  112. .EmployeeTableTD
  113. {
  114. border - width: 1 px;
  115. padding: 8 px;
  116. border - style: solid;
  117. border - color: #666666;
  118. background-color: # ffffff;
  119. } <
  120. /style>
Now Run Application:

Application

Application

Read more articles on ASP.NET: