Models In ASP.NET MVC 5 Using Entity Framework

In this article, we will use Entity Framework with ASP.NET MVC5.

In my previous article Models in ASP.NET MVC5, I have hardcoded the Model EmployeeInfo.cs properties in EmployeeinfoController by creating the instance of the EmployeeInfo Model class.
 
In this article, I am using Entity Framework to retrieve the EmployeeInfo properties data from the database table and render the data to the view. Before reading this article I suggest you go through my article Models in ASP.NET MVC5 for reference.
 
First of all, I am creating a table in SQL Server database with the name as "[dbo].[EmployeeDetails_Department]" as shown below. 
  1. GO  
  2.   
  3. /****** Object:  Table [dbo].[EmployeeDetails_Department]    Script Date: 2019-08-17 3:30:47 PM ******/  
  4. SET ANSI_NULLS ON  
  5. GO  
  6.   
  7. SET QUOTED_IDENTIFIER ON  
  8. GO  
  9.   
  10. CREATE TABLE [dbo].[EmployeeDetails_Department](  
  11.     [EmployeeId] [intNULL,  
  12.     [EmployeeName] [varchar](50) NULL,  
  13.     [EmployeeDesignation] [varchar](50) NULL,  
  14.     [DateOfJoining] [varchar](50) NULL,  
  15.     [EmployeeAddress] [varchar](50) NULL,  
  16.     [EmployeePassport] [varchar](50) NULL,  
  17.     [EmployeePhone] [intNULL,  
  18.     [EmployeeAge] [intNULL,  
  19.     [EmployeeGender] [varchar](10) NULL,  
  20.     [City] [varchar](50) NULL,  
  21.     [Project] [varchar](50) NULL,  
  22.     [EmployeeBloodGroup] [varchar](10) NULL,  
  23.     [CompanyName] [varchar](50) NULL,  
  24.     [PinCode] [intNULL,  
  25.     [DepartmentId] [intNULL  
  26. ON [PRIMARY]  
  27. GO  
After creation of the above table, I have inserted a few records into the table in order to render the data to the HTML table.
 
Models In ASP.NET MVC 5 Using Entity Framework
 
Let's create an ASP.NET MVC5 Web Application.

Open Visual Studio 2017 or any version of your choice. In my case, I am using Visual Studio 2017.
 
Click on File --> New --> Project. Select "Web" from the left window. From the right window, select "ASP.Net Web Application (.NET Framework)". Give some name for the application & click OK. Select the MVC template and click OK.

Once the application is ready, create a Model class with the name as "EmployeeInfo" by adding the properties same as the table's
column names. If any of the property is missing or the property name changes with respect to the table column then the application throws an error with the invalid  column name. For the creation of a project or adding Models, Controller class please refer my previous article for reference.
 
Suppose we use 12 columns in a table, then we need to use the same 12 properties with the same name as table's column names.
 
In order to create a Model class right-click on Models folder, hover on Add button, and click on the class. From "Add New Item" Window select class template and give the Name as "EmployeeInfo.cs" and click on Add button, with this EmployeeInfo.cs Model class will be created. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.ComponentModel.DataAnnotations.Schema;  
  5. using System.Linq;  
  6. using System.Web;  
  7.   
  8. namespace ModelsWithEntityFramework.Models  
  9. {  
  10.     [Table("EmployeeDetails_Department")]  
  11.     public class EmployeeInfo  
  12.     {  
  13.         [Key]  
  14.         public int EmployeeId { getset; }  
  15.         public string EmployeeName { getset; }  
  16.         public string EmployeeDesignation { getset; }  
  17.         public string DateOfJoining { getset; }  
  18.         public string EmployeeAddress { getset; }  
  19.         public string EmployeePassport { getset; }  
  20.         public int EmployeePhone { getset; }  
  21.         public int EmployeeAge { getset; }  
  22.         public string EmployeeGender { getset; }  
  23.         public string City { getset; }  
  24.         public string Project { getset; }  
  25.         public string EmployeeBloodGroup { getset; }  
  26.         public string CompanyName { getset; }  
  27.         public int PinCode { getset; }  
  28.         public int DepartmentId{ getset; }  
  29.     }  
  30. }  
 Models In ASP.NET MVC 5 Using Entity Framework
 
From the above EmployeeInfo Model class, we use Table attribute which is placed on EmployeeInfo class. Here we need to give the database table name as the parameter for the Table attribute with which database table we need to interact.
 
To add an Entity Framework reference, right-click on References click on Manage Nuget Packages and browse for Entity Framework,
select Entity Framework and click on Install.
 
Models In ASP.NET MVC 5 Using Entity Framework
 
With this, the reference will be added to the project's solution.
 
Models In ASP.NET MVC 5 Using Entity Framework
 
Right click on Models folder to add EmployeeContext.cs class. 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Data.Entity;  
  6.   
  7. namespace ModelsWithEntityFramework.Models  
  8. {  
  9.     public class EmployeeContext : DbContext  
  10.     {  
  11.         public DbSet<EmployeeInfo> dataContext { getset; }  
  12.     }  
  13. }  
Here we don't need any ADO.Net code for interacting with the database and retrieving the data from the database table.
Entity Framework internally does it by interacting with the database by using DbSet. 
 
DbSet<EmployeeInfo>  maps with the database table column. If any of the EmployeeInfo properties are missing or properties don't
match with the database table columns then it throws an error with the invalid column name.
 
In web.config use the below code.
  1. <connectionStrings>  
  2.  <add name="EmployeeContext" connectionString="Data Source=KHAJAMOIZ\SSMS17;Initial Catalog=PracticeDB;Integrated Security=True" providerName="System.Data.SqlClient"/>  
  3. </connectionStrings>  
In the above ConnectionString we need to define the name of the Context class, in our case we need to define it as EmployeeContext.
 
In this article, I am using three different scenarios for binding the data to the HTML table.

In the first scenario, I am retrieving all the Employees by interacting with the database.

In the second scenario, I am retrieving the employees who belong to the particular department & In the third scenario, I am retrieving the employee with their employeeId.
 
In order to do this, I am creating a Controller by right-clicking on Controller's folder, hover on Add and click on Controller,
select MVC5 Controller - Empty template. In Add Controller's window give the Controller Name as EmployeeController and click on Add.
With this, a new EmployeeController is added to the Controller's folder.
 
Write the below code in EmployeeController.cs,
  1. using ModelsWithEntityFramework.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 ModelsWithEntityFramework.Controllers  
  9. {  
  10.     public class EmployeeController : Controller  
  11.     {  
  12.         private readonly EmployeeContext empContext = new EmployeeContext();  
  13.         // GET: Employee  
  14.         public ActionResult GetEmployeeInfo()  
  15.         {  
  16.             List<EmployeeInfo> employeeInfos = empContext.dataContext.ToList();  
  17.             return View(employeeInfos);  
  18.         }  
  19.   
  20.         public ActionResult GetEmployeeInfoByDeptId(int id)  
  21.         {  
  22.             List<EmployeeInfo> employeeByDept = empContext.dataContext.Where(x=>x.DepartmentId== id).ToList();  
  23.             return View(employeeByDept);  
  24.         }  
  25.   
  26.         public ActionResult GetEmployeeDetails(int id)  
  27.         {  
  28.             EmployeeInfo employeeInfos = empContext.dataContext.SingleOrDefault(x=>x.EmployeeId == id);  
  29.             return View(employeeInfos);  
  30.         }  
  31.     }  
  32. }  
In the above code, I am creating an instance of EmployeeContext as private readonly at once.
 
Models In ASP.NET MVC 5 Using Entity Framework
 
In the above Controller, I have used three different ActionMethods. 
  1. GetEmployeeInfo: To Retrieve all the Employee records.
  2. GetEmployeeInfoByDeptId: To retrieve Employees who belong to the particular department.
  3. GetEmployeeDetails: To retrieve Employee based on EmployeeId.
Based on the above-mentioned scenarios, I am using 3 different ActionMethods. In order to add view, right-click on GetEmployeeInfo Action method, click on Add View and give the names as shown in below image. 
 
Models In ASP.NET MVC 5 Using Entity Framework 
 
With this GetEmployeeInfo.cshtml file will be added under Views folder. Open the GetEmployeeInfo.cshtml file and place the below code.
  1. @model IEnumerable<ModelsWithEntityFramework.Models.EmployeeInfo>  
  2. @using ModelsWithEntityFramework.Models;  
  3.   
  4. @{  
  5.     ViewBag.Title = "GetEmployeeInfo";  
  6. }  
  7.   
  8. <h2>Get All Employee Information.</h2>  
  9.   
  10. <style>  
  11.     table {  
  12.         font-family: arial, sans-serif;  
  13.         border-collapse: collapse;  
  14.         width: 100%;  
  15.     }  
  16.   
  17.     td, th {  
  18.         border: 1px solid #dddddd;  
  19.         text-align: left;  
  20.         padding: 8px;  
  21.     }  
  22. </style>  
  23.   
  24. <h2>List Of Employees Using Model Using Entity Framework</h2>  
  25. <hr />  
  26.   
  27. <table>  
  28.     <tr>  
  29.         <th>EmployeeId</th>  
  30.         <th>EmployeeName</th>  
  31.         <th>EmployeeDesignation</th>  
  32.         <th>DateOfJoining</th>  
  33.         <th>EmployeeAddress</th>  
  34.         <th>EmployeePassport</th>  
  35.         <th>EmployeePhone</th>  
  36.         <th>EmployeeAge</th>  
  37.         <th>EmployeeGender</th>  
  38.         <th>City</th>  
  39.         <th>Project</th>  
  40.         <th>EmployeeBloodGroup</th>  
  41.         <th>CompanyName</th>  
  42.         <th>PinCode</th>  
  43.         <th>DepartmentId</th>  
  44.     </tr>  
  45.   
  46.     @foreach (EmployeeInfo employeeInfo in Model)  
  47.     {  
  48.         <tr>  
  49.   
  50.             <td>@employeeInfo.EmployeeId</td>  
  51.             <td>@employeeInfo.EmployeeName</td>  
  52.             <td>@employeeInfo.EmployeeDesignation</td>  
  53.             <td>@employeeInfo.DateOfJoining</td>  
  54.             <td>@employeeInfo.EmployeeAddress</td>  
  55.             <td>@employeeInfo.EmployeePassport</td>  
  56.             <td>@employeeInfo.EmployeePhone</td>  
  57.             <td>@employeeInfo.EmployeeAge</td>  
  58.             <td>@employeeInfo.EmployeeGender</td>  
  59.             <td>@employeeInfo.City</td>  
  60.             <td>@employeeInfo.Project</td>  
  61.             <td>@employeeInfo.EmployeeBloodGroup</td>  
  62.             <td>@employeeInfo.CompanyName</td>  
  63.             <td>@employeeInfo.PinCode</td>  
  64.             <td>@employeeInfo.DepartmentId</td>  
  65.   
  66.         </tr>  
  67.     }  
  68. </table>  
  69.   
  70.   
  71. <h2>List Of Employees Using HTML Helper Method & Entity Framework</h2>  
  72. <hr />  
  73.   
  74. <table>  
  75.     <tr>  
  76.         <th>@Html.DisplayNameFor(x => x.EmployeeId)</th>  
  77.         <th>@Html.DisplayNameFor(x => x.EmployeeName)</th>  
  78.         <th>@Html.DisplayNameFor(x => x.EmployeeDesignation)</th>  
  79.         <th>@Html.DisplayNameFor(x => x.DateOfJoining)</th>  
  80.         <th>@Html.DisplayNameFor(x => x.EmployeeAddress)</th>  
  81.         <th>@Html.DisplayNameFor(x => x.EmployeePassport)</th>  
  82.         <th>@Html.DisplayNameFor(x => x.EmployeePhone)</th>  
  83.         <th>@Html.DisplayNameFor(x => x.EmployeeAge)</th>  
  84.         <th>@Html.DisplayNameFor(x => x.EmployeeGender)</th>  
  85.         <th>@Html.DisplayNameFor(x => x.City)</th>  
  86.         <th>@Html.DisplayNameFor(x => x.Project)</th>  
  87.         <th>@Html.DisplayNameFor(x => x.EmployeeBloodGroup)</th>  
  88.         <th>@Html.DisplayNameFor(x => x.CompanyName)</th>  
  89.         <th>@Html.DisplayNameFor(x => x.PinCode)</th>  
  90.         <th>@Html.DisplayNameFor(x => x.DepartmentId)</th>  
  91.     </tr>  
  92.   
  93.     @foreach (EmployeeInfo employInfo in Model)  
  94.     {  
  95.         <tr>  
  96.             <td>@Html.DisplayFor(x => employInfo.EmployeeId)</td>  
  97.             <td>@Html.DisplayFor(x => employInfo.EmployeeName)</td>  
  98.             <td>@Html.DisplayFor(x => employInfo.EmployeeDesignation)</td>  
  99.             <td>@Html.DisplayFor(x => employInfo.DateOfJoining)</td>  
  100.             <td>@Html.DisplayFor(x => employInfo.EmployeeAddress)</td>  
  101.             <td>@Html.DisplayFor(x => employInfo.EmployeePassport)</td>  
  102.             <td>@Html.DisplayFor(x => employInfo.EmployeePhone)</td>  
  103.             <td>@Html.DisplayFor(x => employInfo.EmployeeAge)</td>  
  104.             <td>@Html.DisplayFor(x => employInfo.EmployeeGender)</td>  
  105.             <td>@Html.DisplayFor(x => employInfo.City)</td>  
  106.             <td>@Html.DisplayFor(x => employInfo.Project)</td>  
  107.             <td>@Html.DisplayFor(x => employInfo.EmployeeBloodGroup)</td>  
  108.             <td>@Html.DisplayFor(x => employInfo.CompanyName)</td>  
  109.             <td>@Html.DisplayFor(x => employInfo.PinCode)</td>  
  110.             <td>@Html.DisplayFor(x => employInfo.DepartmentId)</td>  
  111.         </tr>  
  112.     }  
  113. </table>  
To add a view, right click on GetEmployeeInfoByDeptId and click on Add View.. and give the names as shown below.
 
Models In ASP.NET MVC 5 Using Entity Framework
 
With this GetEmployeeInfoByDepartmentId.cshtml file is added under Views folder. Open the GetEmployeeInfoByDepartmentId.cshtml file and place the following code.  
  1. @model IEnumerable<ModelsWithEntityFramework.Models.EmployeeInfo>    
  2. @using ModelsWithEntityFramework.Models;    
  3.     
  4. @{    
  5.     ViewBag.Title = "EmployeeInfoList";    
  6. }    
  7.     
  8. <h2>Get Employee Info based on DepartmentId</h2>    
  9.     
  10. <style>    
  11.     table {    
  12.         font-family: arial, sans-serif;    
  13.         border-collapse: collapse;    
  14.         width: 100%;    
  15.     }    
  16.     
  17.     td, th {    
  18.         border: 1px solid #dddddd;    
  19.         text-align: left;    
  20.         padding: 8px;    
  21.     }    
  22. </style>    
  23.     
  24. <h2>List Of Employees Using Model Using Entity Framework</h2>    
  25. <hr />    
  26.     
  27. <table>    
  28.     <tr>    
  29.         <th>EmployeeId</th>    
  30.         <th>EmployeeName</th>    
  31.         <th>EmployeeDesignation</th>    
  32.         <th>DateOfJoining</th>    
  33.         <th>EmployeeAddress</th>    
  34.         <th>EmployeePassport</th>    
  35.         <th>EmployeePhone</th>    
  36.         <th>EmployeeAge</th>    
  37.         <th>EmployeeGender</th>    
  38.         <th>City</th>    
  39.         <th>Project</th>    
  40.         <th>EmployeeBloodGroup</th>    
  41.         <th>CompanyName</th>    
  42.         <th>PinCode</th>    
  43.         <th>DepartmentId</th>    
  44.     </tr>    
  45.     
  46.     @foreach (EmployeeInfo employeeInfo in Model)    
  47.     {    
  48.         <tr>    
  49.     
  50.             <td>@employeeInfo.EmployeeId</td>    
  51.             <td>@employeeInfo.EmployeeName</td>    
  52.             <td>@employeeInfo.EmployeeDesignation</td>    
  53.             <td>@employeeInfo.DateOfJoining</td>    
  54.             <td>@employeeInfo.EmployeeAddress</td>    
  55.             <td>@employeeInfo.EmployeePassport</td>    
  56.             <td>@employeeInfo.EmployeePhone</td>    
  57.             <td>@employeeInfo.EmployeeAge</td>    
  58.             <td>@employeeInfo.EmployeeGender</td>    
  59.             <td>@employeeInfo.City</td>    
  60.             <td>@employeeInfo.Project</td>    
  61.             <td>@employeeInfo.EmployeeBloodGroup</td>    
  62.             <td>@employeeInfo.CompanyName</td>    
  63.             <td>@employeeInfo.PinCode</td>    
  64.             <td>@employeeInfo.DepartmentId</td>    
  65.     
  66.         </tr>    
  67.     }    
  68. </table>    
  69.     
  70.     
  71. <h2>List Of Employees Using HTML Helper Method & Entity Framework</h2>    
  72. <hr />    
  73.     
  74. <table>    
  75.     <tr>    
  76.         <th>@Html.DisplayNameFor(x => x.EmployeeId)</th>    
  77.         <th>@Html.DisplayNameFor(x => x.EmployeeName)</th>    
  78.         <th>@Html.DisplayNameFor(x => x.EmployeeDesignation)</th>    
  79.         <th>@Html.DisplayNameFor(x => x.DateOfJoining)</th>    
  80.         <th>@Html.DisplayNameFor(x => x.EmployeeAddress)</th>    
  81.         <th>@Html.DisplayNameFor(x => x.EmployeePassport)</th>    
  82.         <th>@Html.DisplayNameFor(x => x.EmployeePhone)</th>    
  83.         <th>@Html.DisplayNameFor(x => x.EmployeeAge)</th>    
  84.         <th>@Html.DisplayNameFor(x => x.EmployeeGender)</th>    
  85.         <th>@Html.DisplayNameFor(x => x.City)</th>    
  86.         <th>@Html.DisplayNameFor(x => x.Project)</th>    
  87.         <th>@Html.DisplayNameFor(x => x.EmployeeBloodGroup)</th>    
  88.         <th>@Html.DisplayNameFor(x => x.CompanyName)</th>    
  89.         <th>@Html.DisplayNameFor(x => x.PinCode)</th>    
  90.         <th>@Html.DisplayNameFor(x=>x.DepartmentId)</th>    
  91.     </tr>    
  92.     
  93.     @foreach (EmployeeInfo employInfo in Model)    
  94.     {    
  95.         <tr>    
  96.             <td>@Html.DisplayFor(x => employInfo.EmployeeId)</td>    
  97.             <td>@Html.DisplayFor(x => employInfo.EmployeeName)</td>    
  98.             <td>@Html.DisplayFor(x => employInfo.EmployeeDesignation)</td>    
  99.             <td>@Html.DisplayFor(x => employInfo.DateOfJoining)</td>    
  100.             <td>@Html.DisplayFor(x => employInfo.EmployeeAddress)</td>    
  101.             <td>@Html.DisplayFor(x => employInfo.EmployeePassport)</td>    
  102.             <td>@Html.DisplayFor(x => employInfo.EmployeePhone)</td>    
  103.             <td>@Html.DisplayFor(x => employInfo.EmployeeAge)</td>    
  104.             <td>@Html.DisplayFor(x => employInfo.EmployeeGender)</td>    
  105.             <td>@Html.DisplayFor(x => employInfo.City)</td>    
  106.             <td>@Html.DisplayFor(x => employInfo.Project)</td>    
  107.             <td>@Html.DisplayFor(x => employInfo.EmployeeBloodGroup)</td>    
  108.             <td>@Html.DisplayFor(x => employInfo.CompanyName)</td>    
  109.             <td>@Html.DisplayFor(x => employInfo.PinCode)</td>    
  110.             <td>@Html.DisplayFor(x=> employInfo.DepartmentId)</td>    
  111.         </tr>    
  112.     }    
  113. </table>     
Similarly, to add a view, right click on GetEmployeeDetails Action method and click on Add View.. button and give the names as
shown below.
 
Models In ASP.NET MVC 5 Using Entity Framework
 
With this GetEmployeeDetails.cshtml file is added in the Views Folder. 
 
Models In ASP.NET MVC 5 Using Entity Framework
 
Open the GetEmployeeDetails.cshtml file and place the following code.  
  1. @model ModelsWithEntityFramework.Models.EmployeeInfo    
  2.     
  3. @{    
  4.     ViewBag.Title = "GetEmployeeDetails";    
  5. }    
  6.     
  7. <h2>GetEmployeeDetails</h2>    
  8.     
  9. <style>    
  10.     table {    
  11.         font-family: arial, sans-serif;    
  12.         border-collapse: collapse;    
  13.         width: 100%;    
  14.     }    
  15.     
  16.     td, th {    
  17.         border: 1px solid #dddddd;    
  18.         text-align: left;    
  19.         padding: 8px;    
  20.     }    
  21. </style>    
  22.     
  23. <h2>GetEmployeeInfo Using Model Entity Framework</h2>    
  24. <hr />    
  25.     
  26.     
  27. <table>    
  28.     <tr>    
  29.         <th>EmployeeId</th>    
  30.         <th>EmployeeName</th>    
  31.         <th>EmployeeDesignation</th>    
  32.         <th>DateOfJoining</th>    
  33.         <th>EmployeeAddress</th>    
  34.         <th>EmployeePassport</th>    
  35.         <th>EmployeePhone</th>    
  36.         <th>EmployeeAge</th>    
  37.         <th>EmployeeGender</th>    
  38.         <th>City</th>    
  39.         <th>Project</th>    
  40.         <th>EmployeeBloodGroup</th>    
  41.         <th>CompanyName</th>    
  42.         <th>PinCode</th>    
  43.         <th>DepartmentId</th>    
  44.     </tr>    
  45.     
  46.     <tr>    
  47.     
  48.         <td>@Model.EmployeeId</td>    
  49.         <td>@Model.EmployeeName</td>    
  50.         <td>@Model.EmployeeDesignation</td>    
  51.         <td>@Model.DateOfJoining</td>    
  52.         <td>@Model.EmployeeAddress</td>    
  53.         <td>@Model.EmployeePassport</td>    
  54.         <td>@Model.EmployeePhone</td>    
  55.         <td>@Model.EmployeeAge</td>    
  56.         <td>@Model.EmployeeGender</td>    
  57.         <td>@Model.City</td>    
  58.         <td>@Model.Project</td>    
  59.         <td>@Model.EmployeeBloodGroup</td>    
  60.         <td>@Model.CompanyName</td>    
  61.         <td>@Model.PinCode</td>    
  62.         <td>@Model.DepartmentId</td>    
  63.     
  64.     </tr>    
  65. </table>    
  66.     
  67. <h2>GetEmployeeInfo Using HTML Helper Method Using Entity Framework</h2>    
  68. <hr />    
  69.     
  70. <table>    
  71.     <tr>    
  72.         <th>@Html.DisplayNameFor(x => x.EmployeeId)</th>    
  73.         <th>@Html.DisplayNameFor(x => x.EmployeeName)</th>    
  74.         <th>@Html.DisplayNameFor(x => x.EmployeeDesignation)</th>    
  75.         <th>@Html.DisplayNameFor(x => x.DateOfJoining)</th>    
  76.         <th>@Html.DisplayNameFor(x => x.EmployeeAddress)</th>    
  77.         <th>@Html.DisplayNameFor(x => x.EmployeePassport)</th>    
  78.         <th>@Html.DisplayNameFor(x => x.EmployeePhone)</th>    
  79.         <th>@Html.DisplayNameFor(x => x.EmployeeAge)</th>    
  80.         <th>@Html.DisplayNameFor(x => x.EmployeeGender)</th>    
  81.         <th>@Html.DisplayNameFor(x => x.City)</th>    
  82.         <th>@Html.DisplayNameFor(x => x.Project)</th>    
  83.         <th>@Html.DisplayNameFor(x => x.EmployeeBloodGroup)</th>    
  84.         <th>@Html.DisplayNameFor(x => x.CompanyName)</th>    
  85.         <th>@Html.DisplayNameFor(x => x.PinCode)</th>    
  86.         <th>@Html.DisplayNameFor(x=>x.DepartmentId)</th>    
  87.     </tr>    
  88.     
  89.     <tr>    
  90.     
  91.         <td>@Html.DisplayFor(x => x.EmployeeId)</td>    
  92.         <td>@Html.DisplayFor(x => x.EmployeeName)</td>    
  93.         <td>@Html.DisplayFor(x => x.EmployeeDesignation)</td>    
  94.         <td>@Html.DisplayFor(x => x.DateOfJoining)</td>    
  95.         <td>@Html.DisplayFor(x => x.EmployeeAddress)</td>    
  96.         <td>@Html.DisplayFor(x => x.EmployeePassport)</td>    
  97.         <td>@Html.DisplayFor(x => x.EmployeePhone)</td>    
  98.         <td>@Html.DisplayFor(x => x.EmployeeAge)</td>    
  99.         <td>@Html.DisplayFor(x => x.EmployeeGender)</td>    
  100.         <td>@Html.DisplayFor(x => x.City)</td>    
  101.         <td>@Html.DisplayFor(x => x.Project)</td>    
  102.         <td>@Html.DisplayFor(x => x.EmployeeBloodGroup)</td>    
  103.         <td>@Html.DisplayFor(x => x.CompanyName)</td>    
  104.         <td>@Html.DisplayFor(x => x.PinCode)</td>    
  105.         <th>@Html.DisplayFor(x => x.DepartmentId)</th>    
  106.     
  107.     </tr>    
  108. </table>    
Now execute the application by navigating to the Url as http://localhost:54816/Employee/GetEmployeeInfo, the first Action method is
executed and the output is shown below. 
 
Models In ASP.NET MVC 5 Using Entity Framework
 
For executing GetEmployeeInfoByDeptId Action method navigate the url as localhost:54816/Employee/GetEmployeeInfoByDeptId/1001,
The above url gets all the employee records who belong to the department 1001.The output for the above request is shown below. 
 
Models In ASP.NET MVC 5 Using Entity Framework
 
Similarly to execute GetEmployeeDetails Action method navigate the URL as localhost:54816/Employee/GetEmployeeDetails/1001.

The above URL gets the employee details with employeeId as 1001 and the output for the above request is shown below. 
 
Models In ASP.NET MVC 5 Using Entity Framework
 
Thanks for reading this article, hope it helps you.