How To Retrieve Data From Database Using jQuery AJAX In MVC 5

Introduction

In this blog, I will demonstrate how to retrieve the data from SQL database using jQuery AJAX in ASP.NET MVC5. I will use jQuery data table for searching, sorting, and paging to HTML table.

Step 1

Open SQL Server 2014 and create a database table.

  1. CREATE TABLE [dbo].[Employees](  
  2.     [ID] [int] IDENTITY(1,1) NOT NULL,  
  3.     [Name] [nvarchar](50) NULL,  
  4.     [Position] [nvarchar](50) NULL,  
  5.     [Office] [nvarchar](50) NULL,  
  6.     [Age] [int] NULL,  
  7.     [Salary] [nvarchar](50) NULL,  
  8.     [Profile_Image] [nvarchar](100) NULL,  
  9.  CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED   
  10. (  
  11.     [ID] ASC  
  12. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]  
  13. ) ON [PRIMARY]  
  14.   
  15. GO  

Step 2

Open Visual Studio 2015, click on New Project, and create an empty web application project.

A New Project one window will appear; select Web from the left panel, choose ASP.NET Web Application, give a meaningful name to your project, and then click on OK. One more window will appear. Choose Empty check on MVC checkbox and click on OK.

Step 3

Click on Tools >> NuGet Package Manager and choose Manage NuGet Packages for Solution. After that, a window will appear. Choose Browse, type bootstrap, and install the package in the project. Similarly, type jQuery, and install the latest version of jQuery package in the project from NuGet. Then, close the NuGet Solution. Keep the useful files in Content and scripts folder.

Step 4

Add Entity Framework, right-click on Models folder, select Add. Then, select New Item and click on it.

After clicking on the New item, you will get a window. From there, select data from the left panel and choose ADO.NET Entity Data Model to give it a name DBModels (this name is not mandatory you can give any name) then click on Add. After you click on Add, a window wizard will open.
Choose EF Designer from database and click Next. After clicking on Next window will appear. Choose a New Connection. Another window will appear. Add your server name. If it is local, then enter dot (.). Choose your database and click on OK. The connection will get added. If you wish to save connect as you want, you can change the name of your connection below.
It will save the connection in web config, then click on Next. After clicking on NEXT, another window will appear. Choose database table name as shown in the below screenshot and click on Finish. Entity framework will be added with the respective class that gets generated under Models folder.

Following class will be added,

  1. namespace MvcJQueryDataTable_Demo.Models  
  2. {  
  3.     using System;  
  4.     using System.Collections.Generic;  
  5.       
  6.     public partial class Employee  
  7.     {  
  8.         public int ID { get; set; }  
  9.         public string Name { get; set; }  
  10.         public string Position { get; set; }  
  11.         public string Office { get; set; }  
  12.         public Nullable<int> Age { get; set; }  
  13.         public string Salary { get; set; }  
  14.     }  
  15. }  

Step 5

Right click on Controllers folder select Add then choose Controller. After click on controller, a window will appear. Choose MVC5 Controller-Empty and click on Add. After clicking on Add, another window will appear with DefaultController change the name HomeController then click on Add. HomeController will be added under Controllers folder. Remember don’t change Controller its suffix for all controller change only highlight instead of Default just change Home.

Add following namespace

  1. using MvcJQueryDataTable_Demo.Models;  

Complete code of HomeController

  1. using MvcJQueryDataTable_Demo.Models;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web.Mvc;  
  5.   
  6. namespace MvcJQueryDataTable_Demo.Controllers  
  7. {  
  8.     public class HomeController : Controller  
  9.     {  
  10.         public ActionResult Index()  
  11.         {  
  12.             return View();                
  13.         }  
  14.         public ActionResult GetData()  
  15.         {  
  16.             using (DBModel db=new DBModel())  
  17.             {  
  18.                 List<Employee> employeeList = db.Employees.ToList<Employee>();  
  19.                 return Json(new { data = employeeList }, JsonRequestBehavior.AllowGet);  
  20.             }  
  21.         }  
  22.     }  
  23. }  

Step 6

Right click on Index action method in controller Add view window will appear with default Index name uncheck (use a Layout page) click on Add. As shown in below screenshot, the View will be added in views folder under Home folder with name Index.

Add the following styles and script file in head section of index view,

  1. <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  2. <script src="~/scripts/jquery-3.3.1.min.js"></script>  
  3. <script src="~/scripts/bootstrap.min.js"></script>  
  4. <link href="~/Content/dataTables.bootstrap4.min.css" rel="stylesheet" />     
  5. <script src="~/scripts/jquery.dataTables.min.js"></script>  
  6. <script src="~/scripts/dataTables.bootstrap4.min.js"></script>  

Write following jquery script to retrieve data from database,

  1. <script type="text/javascript">  
  2.         $(document).ready(function () {  
  3.             $('#dataTable').DataTable({  
  4.                 "ajax": {  
  5.                     "url""/Home/GetData",  
  6.                     "type""GET",  
  7.                     "datatype""json"  
  8.                 },  
  9.                 "columns": [  
  10.                     { "data""Name" },  
  11.                     { "data""Position" },  
  12.                     { "data""Office" },  
  13.                     { "data""Age" },  
  14.                     { "data""Salary" }  
  15.                 ]  
  16.             });  
  17.         });           
  18.     </script>  

Design the HTML table in index view to display data,

  1. <body>  
  2.     <div class="container py-4">  
  3.         <h4 class="text-center text-uppercase">HOW TO RETRIEVE DATA FROM DATABASE USING JQUERY AJAX IN MVC5</h4>  
  4.         <div id="dataTable_wrapper"></div>  
  5.         <table id="dataTable" class="table table-bordered table-striped">  
  6.             <thead>  
  7.                 <tr>  
  8.                     <th>Name</th>  
  9.                     <th>Position</th>  
  10.                     <th>Office</th>  
  11.                     <th>Age</th>  
  12.                     <th>Salary</th>  
  13.                 </tr>  
  14.             </thead>  
  15.         </table>  
  16.     </div>  
  17. </body>  

Complete index view code

  1. @{  
  2.     Layout = null;  
  3. }  
  4.   
  5. <!DOCTYPE html>  
  6.   
  7. <html>  
  8. <head>  
  9.     <meta name="viewport" content="width=device-width" />  
  10.     <title>Index</title>  
  11.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  12.     <script src="~/scripts/jquery-3.3.1.min.js"></script>  
  13.     <script src="~/scripts/bootstrap.min.js"></script>  
  14.     <link href="~/Content/dataTables.bootstrap4.min.css" rel="stylesheet" />     
  15.     <script src="~/scripts/jquery.dataTables.min.js"></script>  
  16.     <script src="~/scripts/dataTables.bootstrap4.min.js"></script>  
  17.     <script type="text/javascript">  
  18.         $(document).ready(function () {  
  19.             $('#dataTable').DataTable({  
  20.                 "ajax": {  
  21.                     "url""/Home/GetData",  
  22.                     "type""GET",  
  23.                     "datatype""json"  
  24.                 },  
  25.                 "columns": [  
  26.                     { "data""Name" },  
  27.                     { "data""Position" },  
  28.                     { "data""Office" },  
  29.                     { "data""Age" },  
  30.                     { "data""Salary" }  
  31.                 ]  
  32.             });  
  33.         });           
  34.     </script>  
  35. </head>  
  36. <body>  
  37.     <div class="container py-4">  
  38.         <h4 class="text-center text-uppercase">HOW TO RETRIEVE DATA FROM DATABASE USING JQUERY AJAX IN MVC5</h4>  
  39.         <div id="dataTable_wrapper"></div>  
  40.         <table id="dataTable" class="table table-bordered table-striped">  
  41.             <thead>  
  42.                 <tr>  
  43.                     <th>Name</th>  
  44.                     <th>Position</th>  
  45.                     <th>Office</th>  
  46.                     <th>Age</th>  
  47.                     <th>Salary</th>  
  48.                 </tr>  
  49.             </thead>  
  50.         </table>  
  51.     </div>  
  52. </body>  
  53. </html>  

Step 7

Run the project by pressing Ctrl +F5.

ASP.NET
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now