How To Dynamically Load Employee Details In jQuery Popover Using MVC 5

Introduction

In this article, I will demonstrate how to dynamically load employee details using MVC 5 and Entity Framework. I will also load employee details in bootstrap popover based on their Id and will also use jQuery data table for searching, sorting, and paging.

Step 1

Open SQL server 2014 and create a database table. 

  1. CREATE TABLE [dbo].[Employee](  
  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] [intNULL,  
  7.     [Satrt_Date] [dateNULL,  
  8.     [Salary] [nvarchar](50) NULL,  
  9.  CONSTRAINT [PK_Employee] 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 = ONON [PRIMARY]  
  13. ON [PRIMARY]  
  14.   
  15. GO  

Step 2

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

Screenshot for creating new project 1

ASP.NET

After clicking on 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 as shown in below screenshot.

Screenshot for creating new project 2

ASP.NET

After clicking on OK one more window will appear; choose empty, check on MVC checkbox and click on OK as shown below screenshot.

Screenshot for creating new project 3

ASP.NET

 

After clicking OK, the project will be created with the name of MvcDynamicallyLoadContent_Demo.

Step 3

Add Entity Framework now. For that, right click on Models folder, select Add, then select New Item, then click on it.

Screenshot for adding entity framework 1

ASP.NET

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, give it the name DBModels (this name is not mandatory you can give any name) and click on Add.

Screenshot for adding entity framework 2

ASP.NET

 

After you click on "Add a window", the wizard will open, choose EF Designer from the database and click Next. 

Screenshot for adding entity framework 3

ASP.NET

 

After clicking on Next a window will appear. Choose New Connection. Another window will appear, add your server name if it is local then enter a dot (.). Choose your database and click on OK.

Screenshot for adding entity framework 4

ASP.NET

 

The connection will be added. If you wish to save connect as you want you can change the name of your connection below. It will save connection in web config then click on Next. 

Screenshot for adding entity framework 5

ASP.NET

After clicking on NEXT another window will appear to choose database table name as shown in the below screenshot then click on Finish.

Screenshot for adding Entity Framework 6

ASP.NET

Screenshot for adding Entity Framework-7

ASP.NET

Entity Framework will be added and respective class gets generated under Models folder.

Screenshot for adding entity framework 8 

  1. namespace MvcDynamicallyLoadContent_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 Nullable<System.DateTime> Satrt_Date { get; set; }  
  14.         public string Salary { get; set; }  
  15.     }  
  16. }  

Step 4

Right click on Controllers folder, select Add, then choose Controller as shown in below screenshot.

ASP.NET

 

After clicking on the controller a window will appear to choose MVC5 Controller-Empty click on Add.

ASP.NET

 

After clicking on Add another window will appear with DefaultController. Change the name to HomeController then click on Add. HomeController will be added under Controllers folder. Remember don’t change the Controller suffix for all controllers, change only highlight, and instead of Default just change Home as shown in the below screenshot.

ASP.NET

 

Complete code for controller 

  1. using MvcDynamicallyLoadContent_Demo.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7.   
  8. namespace MvcDynamicallyLoadContent_Demo.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         // GET: Home  
  13.         public ActionResult Index()  
  14.         {  
  15.             List<Employee> employeeList = new List<Employee>();  
  16.             using (DBModel db=new DBModel ())  
  17.             {  
  18.                 employeeList= db.Employees.ToList();  
  19.             }  
  20.               return View(employeeList);  
  21.         }  
  22.   
  23.         public ActionResult EmployeeDetails(int id)  
  24.         {  
  25.             using (DBModel db=new DBModel())  
  26.             {  
  27.                 List<Employee> employee = db.Employees.Where(x => x.ID == id).ToList();  
  28.                 return View(employee);  
  29.             }  
  30.         }  
  31.     }  
  32. }  

Step 5

Right-click on index action method in the controller. Add view window will appear with default index name unchecked (use a Layout page) and click on Add as shown in the below screenshot. View will be added in views folder under Home folder with name index.

Screenshot for adding view

ASP.NET

 

Step 6

Design view with HTML, cshtml and bootstrap 4 classes,

Complete index view code 

  1. @model IEnumerable<MvcDynamicallyLoadContent_Demo.Models.Employee>  
  2. @{  
  3.     Layout = null;  
  4. }  
  5.   
  6. <!DOCTYPE html>  
  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/popper.min.js"></script>  
  14.     <script src="~/scripts/bootstrap.min.js"></script>  
  15.     <link href="~/Content/dataTables.bootstrap4.min.css" rel="stylesheet" />  
  16.     <script src="~/scripts/jquery.dataTables.min.js"></script>  
  17.     <script src="~/scripts/dataTables.bootstrap4.min.js"></script>  
  18.     <script type="text/javascript">  
  19.         $(document).ready(function () {  
  20.             $('#DataTable').DataTable();  
  21.             $('[data-toggle="popover"]').popover({  
  22.                 title: setData,  
  23.                 html: true,  
  24.                 placement: 'right'  
  25.             });  
  26.             function setData(id) {  
  27.                 var set_data = '';  
  28.                 var element = $(this);  
  29.                 var id = element.attr("id");  
  30.                 $.ajax({  
  31.                     url: "/Home/EmployeeDetails?id" + id,  
  32.                     method: "post",  
  33.                     async: false,  
  34.                     data: { id: id },  
  35.                     success: function (data) {  
  36.                         set_data = data;  
  37.                     }  
  38.                 });  
  39.                 return set_data;  
  40.             }  
  41.         });  
  42.     </script>  
  43. </head>  
  44. <body>  
  45.     <div class="container py-4">  
  46.         <h4 class="text-center text-uppercase">How Dynamically load employee details in jquery popover in mvc 5</h4>  
  47.         <table id="DataTable" class="table table-bordered table-striped">  
  48.             <thead>  
  49.                 <tr>  
  50.                     <th>ID</th>  
  51.                     <th>Name</th>  
  52.                 </tr>  
  53.             </thead>  
  54.             <tbody>  
  55.                 @foreach (var employee in Model)  
  56.                 {  
  57.                     <tr>  
  58.                         <td>@employee.ID</td>  
  59.                         <td><a href="#" data-toggle="popover" data-trigger="hover" id="@employee.ID">@employee.Name</a></td>  
  60.                     </tr>  
  61.                 }  
  62.             </tbody>  
  63.         </table>  
  64.     </div>  
  65. </body>  
  66. </html>  

Step 7

Create another view right-click on EmployeeDetails action method

  1. @model IEnumerable<MvcDynamicallyLoadContent_Demo.Models.Employee>  
  2. @{  
  3.     Layout = null;  
  4. }  
  5.   
  6. <!DOCTYPE html>  
  7.   
  8. <html>  
  9. <head>  
  10.     <meta name="viewport" content="width=device-width" />  
  11.     <title>EmployeeDetails</title>  
  12.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  13.     <script src="~/scripts/jquery-3.3.1.min.js"></script>  
  14.     <script src="~/scripts/bootstrap.min.js"></script>  
  15. </head>  
  16. <body>  
  17.     <div class="container">  
  18.         <ul class="list-group">  
  19.             @foreach (var employee in Model)  
  20.             {  
  21.                 <li class="list-group-item">@employee.ID</li>  
  22.                 <li class="list-group-item">@employee.Name</li>  
  23.                     <li class="list-group-item">@employee.Position</li>  
  24.                     <li class="list-group-item">@employee.Office</li>  
  25.                     <li class="list-group-item">@employee.Age</li>  
  26.                     <li class="list-group-item">@employee.Satrt_Date</li>  
  27.                     <li class="list-group-item">@employee.Salary</li>  
  28.             }  
  29.         </ul>  
  30.     </div>  
  31. </body>  
  32. </html>   

Step 8

Run Project ctrl+F5,

Screenshot 1

ASP.NET

Screenshot 2

ASP.NET

 


Similar Articles