CRUD Operation Using Model, View, And Controller

In this article, we learn how to do basic CRUD operation using Model, View, and Controller.

In this artcle we will do Insert, Update, Delete and Details of Employee using CRUD operation in MVC.
 
I've used Visual Studio 2015 and SQL Server 2012.
 

Database

 
First we create table t_Employee and also create a below stored procedure. 
  1. CREATE TABLE [dbo].[t_Employee](    
  2.     [ID] [int] IDENTITY(1,1) NOT NULL,    
  3.     [EmpName] [nvarchar](50) NULL,    
  4.     [Address] [nvarchar](50) NULL,    
  5.     [Gender] [nvarchar](50) NULL,      
  6.     [Active] [bitNULL,    
  7.  CONSTRAINT [PK_t_Employee] PRIMARY KEY CLUSTERED     
  8. (    
  9.     [ID] ASC    
  10. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]    
  11. ON [PRIMARY]    
Create Add, Get, Update(Edit), Delete, Details of Employee Sp listed below.
  1. CREATE PROCEDURE usp_AddEmployee        
  2. @EmpName NVARCHAR(50),            
  3. @Address NVARCHAR(50),              
  4. @Gender NVARCHAR(50),        
  5. @Active NVARCHAR(50)        
  6. AS        
  7. BEGIN        
  8.  INSERT INTO t_Employee (EmpName,Address,Gender,Password,ConfirmPassword,Active)       
  9.  VALUES (@EmpName,@Address,@Gender,@Active)        
  10. END     
  11.   
  12. CREATE PROCEDURE usp_GetAllEMployee    
  13. AS    
  14. BEGIN    
  15. SELECT ID, EmpName,Address,Gender,Active from t_Employee    
  16. END  
  17.     
  18. CREATE PROCEDURE usp_UpdateEmployee        
  19. @ID int,        
  20. @EmpName NVARCHAR(50),        
  21. @Address NVARCHAR(50),       
  22. @Gender NVARCHAR(50),        
  23. @Active bit        
  24. AS        
  25. BEGIN        
  26.  UPDATE t_Employee        
  27.  SET EmpName=@EmpName,        
  28.  Address= @Address,                
  29.  Gender= @Gender,        
  30.  Active= @Active        
  31.  WHERE ID=@ID        
  32. END  
  33.   
  34. CREATE PROCEDURE usp_DeleteEMployee    
  35. @ID int    
  36. AS    
  37. BEGIN    
  38.  Delete From t_Employee    
  39.  WHERE ID=@ID    
  40. END  

Visual Studio 2015

 
In Visual studio 2015, First  Select 'ASP.NET Web Application' And give Na ame and Location of Project As below. 
 
CRUD Operation Using Model View And Controller
 
Select MVC and click on Change Authentication. After that select No Authentication becasue we will just focus on CRUD operations in MVC.
 
CRUD Operation Using Model View And Controller
 
After creating a project, in Solution Explorer, right click on "Model" folder and select "Add New Item".  Add one class name "Employee.cs".
 
CRUD Operation Using Model View And Controller
 
And add below code where we can check data annotation which fileld Required, length, Datatype, Password, Confirmpassword etc. See below.
  1. public class Employee  
  2.    {  
  3.        [Key]  
  4.        public int ID { getset; }  
  5.   
  6.        [Required(ErrorMessage = "Please Enter Employee Name")]  
  7.        public string EmpName { getset; }  
  8.   
  9.        [Required(ErrorMessage = "Please Enter Employee Address")]  
  10.        public string Address { getset; }  
  11.   
  12.        [Required(ErrorMessage = "Please Enter Gender")]  
  13.        public string Gender { getset; }    
  14.   
  15.        public bool Active { getset; }  
  16.   
  17.    }  
Right click on the Controller class add "MVC Controller -Empty" and give it a name, "EmployeeController".
 
 CRUD Operation Using Model View And Controller 
  1. public class EmployeeController : Controller    
  2.    {    
  3.        // GET: Employees    
  4.        public ActionResult Index()    
  5.        {    
  6.            return View();    
  7.        }    
  8.    }    
Remove the above code and replace with the following code in EmployeeController.
  1. using System;  
  2. using System.Web.Mvc;  
  3. using CRUD.Models;  
  4. using CRUD.Repository;  
  5. using System.Data;  
  6. using Newtonsoft.Json;  
  7. using System.Net;  
  8. using System.Web.Services;  
  1.  public class EmployeeController : Controller  
  2.     {  
  3.   
  4.         public ActionResult AddEmployee()  
  5.         {  
  6.             try  
  7.             {  
  8.                 return View();  
  9.             }  
  10.             catch (Exception ex)  
  11.             {  
  12.                 throw ex;  
  13.             }  
  14.         }  
  15.   
  16.         [HttpPost]  
  17.         public ActionResult AddEmployee(Employee Emp)  
  18.         {  
  19.             try  
  20.             {  
  21.                 if (ModelState.IsValid)  
  22.                 {  
  23.                     EmployeeDbContext emprep = new EmployeeDbContext();  
  24.                     if (emprep.AddEmployee(Emp))  
  25.                     {  
  26.                         ViewBag.Message = "Record Saved Successfully";  
  27.                     }  
  28.                 }  
  29.                 return RedirectToAction("GetAllEmployee");  
  30.             }  
  31.             catch (Exception ex)  
  32.             {  
  33.                 throw ex;  
  34.             }  
  35.         }  
  36.   
  37.         public ActionResult EditEmployee(int id)  
  38.         {  
  39.             try  
  40.             {  
  41.                 EmployeeDbContext emprep = new EmployeeDbContext();  
  42.                 return View(emprep.GetAllEmployee().Find(Emp => Emp.ID == id));  
  43.             }  
  44.             catch (Exception ex)  
  45.             {  
  46.                 throw ex;  
  47.             }  
  48.         }  
  49.   
  50.         [HttpPost]  
  51.         public ActionResult EditEmployee(int id, Employee Emp)  
  52.         {  
  53.             try  
  54.             {  
  55.                 if (ModelState.IsValid)  
  56.                 {  
  57.                     EmployeeDbContext emprep = new EmployeeDbContext();  
  58.                     if (emprep.EditEmployee(id, Emp))  
  59.                     {  
  60.                         ViewBag.Message = "Record Updated Successfully";  
  61.                     }  
  62.                 }  
  63.                 return RedirectToAction("GetAllEmployee");  
  64.             }  
  65.             catch (Exception ex)  
  66.             {  
  67.                 throw ex;  
  68.             }  
  69.         }         
  70.   
  71.         public ActionResult GetAllEmployee(Employee Emp)  
  72.         {  
  73.             try  
  74.             {  
  75.                 EmployeeDbContext emprep = new EmployeeDbContext();  
  76.                 ModelState.Clear();  
  77.                 return View(emprep.GetAllEmployee());  
  78.             }  
  79.             catch (Exception ex)  
  80.             {  
  81.                 throw ex;  
  82.             }  
  83.         }  
  84.   
  85.         public ActionResult GetDetails(int id)  
  86.         {  
  87.             try  
  88.             {  
  89.                 EmployeeDbContext emprep = new EmployeeDbContext();  
  90.                 return View(emprep.GetAllEmployee().Find(Emp => Emp.ID == id));  
  91.             }  
  92.             catch (Exception ex)  
  93.             {  
  94.                 throw ex;  
  95.             }  
  96.         }  
  97.   
  98.         public ActionResult DeleteEmployee(int? id)  
  99.         {  
  100.             try  
  101.             {  
  102.                 EmployeeDbContext Emprep = new EmployeeDbContext();  
  103.                 return View(Emprep.GetAllEmployee().Find(x => x.ID == id));  
  104.             }  
  105.             catch (Exception ex)  
  106.             {  
  107.                 throw ex;  
  108.             }  
  109.         }  
  110.   
  111.         [HttpPost]  
  112.         public ActionResult DeleteEmployee(int id)  
  113.         {  
  114.             try  
  115.             {  
  116.                 if (ModelState.IsValid)  
  117.                 {  
  118.                     EmployeeDbContext emprep = new EmployeeDbContext();  
  119.                     if (emprep.DeleteEmployee(id))  
  120.                     {  
  121.                         ViewBag.Message = "Record Deleted Successfully";  
  122.                     }  
  123.                 }  
  124.                 return RedirectToAction("GetAllEmployee");  
  125.             }  
  126.             catch (Exception ex)  
  127.             {  
  128.                 throw ex;  
  129.             }  
  130.         }  
  131.   
  132. }  
Add one folder repository and create EmployeeDbContext Class . Add the following code to the class. 
  1. using CRUD.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Configuration;  
  5. using System.Data;  
  6. using System.Data.SqlClient;  
  1. public class EmployeeDbContext  
  2.    {  
  3.        string constr;  
  4.        public EmployeeDbContext()  
  5.        {  
  6.            try  
  7.            {  
  8.                constr = ConfigurationManager.ConnectionStrings["Sqlconn"].ToString();  
  9.            }  
  10.            catch (Exception ex)  
  11.            {  
  12.                throw ex;  
  13.            }  
  14.        }  
  15.   
  16.        public List<Employee> GetAllEmployee()  
  17.        {  
  18.            DataTable dt = new DataTable();  
  19.            List<Employee> emp = new List<Employee>();  
  20.            try  
  21.            {  
  22.   
  23.                using (var con = new SqlConnection(constr))  
  24.                {  
  25.                    using (var cmd = new SqlCommand("usp_GetAllEMployee", con))  
  26.                    {  
  27.                        cmd.CommandType = System.Data.CommandType.StoredProcedure;  
  28.                        SqlDataAdapter ds = new SqlDataAdapter(cmd);  
  29.                        con.Open();  
  30.                        ds.Fill(dt);  
  31.                        foreach (DataRow dr in dt.Rows)  
  32.                        {  
  33.                            emp.Add(  
  34.                                new Employee  
  35.                                {  
  36.                                    ID = Convert.ToInt32(dr["ID"]),  
  37.                                    EmpName = Convert.ToString(dr["EMPName"]),  
  38.                                    Address = Convert.ToString(dr["Address"]),   
  39.                                    Gender = Convert.ToString(dr["Gender"]),  
  40.                                    Active = Convert.ToBoolean(dr["Active"])  
  41.                                }  
  42.                                );  
  43.                        };  
  44.                    }  
  45.                }  
  46.                return emp;  
  47.            }  
  48.            catch (Exception ex)  
  49.            {  
  50.                throw ex;  
  51.            }  
  52.        }  
  53.        public bool AddEmployee(Employee Emp)  
  54.        {  
  55.            bool result = false;  
  56.            try  
  57.            {  
  58.                using (var con = new SqlConnection(constr))  
  59.                {  
  60.                    using (var cmd = new SqlCommand("usp_AddEmployee", con))  
  61.                    {  
  62.                        cmd.CommandType = CommandType.StoredProcedure;  
  63.                        cmd.Parameters.AddWithValue("@EmpName", Emp.EmpName);  
  64.                        cmd.Parameters.AddWithValue("@Address", Emp.Address);  
  65.                        cmd.Parameters.AddWithValue("@Gender", Emp.Gender);   
  66.                        cmd.Parameters.AddWithValue("@Active", Emp.Active);  
  67.                        con.Open();  
  68.                        int i = cmd.ExecuteNonQuery();  
  69.                        con.Close();  
  70.                        if (i > 0)  
  71.                        {  
  72.                            result = true;  
  73.                        }  
  74.                        else  
  75.                        {  
  76.                            result = false;  
  77.                        }  
  78.                    }  
  79.                }  
  80.                return result;  
  81.            }  
  82.            catch (Exception ex)  
  83.            {  
  84.                throw ex;  
  85.            }  
  86.        }  
  87.   
  88.        public bool EditEmployee(int id, Employee Emp)  
  89.        {  
  90.            bool result = false;  
  91.            try  
  92.            {  
  93.                using (var con = new SqlConnection(constr))  
  94.                {  
  95.                    using (var cmd = new SqlCommand("usp_UpdateEmployee", con))  
  96.                    {  
  97.                        cmd.CommandType = CommandType.StoredProcedure;  
  98.                        cmd.Parameters.AddWithValue("@ID", id);  
  99.                        cmd.Parameters.AddWithValue("@EmpName", Emp.EmpName);  
  100.                        cmd.Parameters.AddWithValue("@Address", Emp.Address);  
  101.                        cmd.Parameters.AddWithValue("@Gender", Emp.Gender);  
  102.                        cmd.Parameters.AddWithValue("@Active", Emp.Active);  
  103.                        con.Open();  
  104.                        int i = cmd.ExecuteNonQuery();  
  105.                        if (i > 0)  
  106.                        {  
  107.                            result = true;  
  108.                        }  
  109.                        else  
  110.                        {  
  111.                            result = false;  
  112.                        }  
  113.                    }  
  114.                }  
  115.                return result;  
  116.            }  
  117.            catch (Exception ex)  
  118.            {  
  119.                throw ex;  
  120.            }  
  121.        }  
  122.   
  123.        public bool DeleteEmployee(int id)  
  124.        {  
  125.            bool result = false;  
  126.            try  
  127.            {  
  128.                using (var con = new SqlConnection(constr))  
  129.                {  
  130.                    using (var cmd = new SqlCommand("usp_DeleteEMployee", con))  
  131.                    {  
  132.                        cmd.CommandType = CommandType.StoredProcedure;  
  133.                        cmd.Parameters.AddWithValue("@ID", id);  
  134.                        con.Open();  
  135.                        int i = cmd.ExecuteNonQuery();  
  136.                        if (i > 0)  
  137.                        {  
  138.                            result = true;  
  139.                        }  
  140.                        else  
  141.                        {  
  142.                            result = false;  
  143.                        }  
  144.                    }  
  145.                }  
  146.                return result;  
  147.            }  
  148.            catch (Exception ex)  
  149.            {  
  150.                throw ex;  
  151.            }  
  152.        }  
  153.   
  154.        public DataSet GetDetails()  
  155.        {  
  156.            DataSet ds = new DataSet();  
  157.            try  
  158.            {  
  159.                using (var conn = new SqlConnection(constr))  
  160.                {  
  161.                    using (var cmd = new SqlCommand("usp_Get_Employee", conn))  
  162.                    {  
  163.                        cmd.CommandType = CommandType.StoredProcedure;  
  164.                        conn.Open();  
  165.                        SqlDataAdapter dt = new SqlDataAdapter(cmd);  
  166.                        dt.Fill(ds);  
  167.                        conn.Close();  
  168.                    }  
  169.                }  
  170.                return ds;  
  171.            }  
  172.            catch (Exception ex)  
  173.            {  
  174.   
  175.                throw ex;  
  176.            }  
  177.        }  
  178.   
  179.    } 
Add below key with databasename, username and password. This connection string will be used to connect with the database.
  1. <add name="Sqlconn" connectionString="data source=servername;Initial Catalog=Test;Persist Security Info=True;uid=username;pwd=password" providerName="System.Data.SqlClient"/>   
Let's add a view.
 
Right click on Controller and add a view using Add View. See below.
 
AddEmployee.cshtml
 
CRUD Operation Using Model View And Controller
  1. @model CRUD.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "AddEmployee";  
  5. }  
  6.   
  7. <h2>@ViewBag.Message</h2>  
  8.   
  9.   
  10. @using (Html.BeginForm())  
  11. {  
  12.     @Html.AntiForgeryToken()  
  13.   
  14.     <div class="form-horizontal">  
  15.         <h4>Employee</h4>  
  16.         <hr />  
  17.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
  18.         <div class="form-group">  
  19.             @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })  
  20.             <div class="col-md-10">  
  21.                 @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })  
  22.                 @Html.ValidationMessageFor(model => model.EmpName, ""new { @class = "text-danger" })  
  23.             </div>  
  24.         </div>  
  25.   
  26.         <div class="form-group">  
  27.             @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })  
  28.             <div class="col-md-10">  
  29.                 @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })  
  30.                 @Html.ValidationMessageFor(model => model.Address, ""new { @class = "text-danger" })  
  31.             </div>  
  32.         </div>  
  33.   
  34.   
  35.         <div class="form-group">  
  36.             @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })  
  37.             <div class="col-md-10">  
  38.                 @Html.DropDownList("Gender"new List<SelectListItem>  
  39.   
  40.                  {  
  41.                     new SelectListItem{ Text="---Select Gender---", Value = "Select Gender" },  
  42.                     new SelectListItem{ Text="Male", Value = "Male" },  
  43.                     new SelectListItem{ Text="Female", Value = "Female" }  
  44.                  }, new { @class = "textbox" })  
  45.             </div>  
  46.         </div>  
  47.   
  48.   
  49.   
  50.         <div class="form-group">  
  51.             @Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label col-md-2" })  
  52.             <div class="col-md-10">  
  53.                 <div class="checkbox">  
  54.                     @Html.EditorFor(model => model.Active)  
  55.                     @Html.ValidationMessageFor(model => model.Active, ""new { @class = "text-danger" })  
  56.                 </div>  
  57.             </div>  
  58.         </div>  
  59.   
  60.         <div class="form-group">  
  61.             <div class="col-md-offset-2 col-md-10">  
  62.                 <input type="submit" value="Create" class="btn btn-default" />  
  63.             </div>  
  64.         </div>  
  65.     </div>  
  66. }  
  67.   
  68. <div>  
  69.     @Html.ActionLink("Back to List""GetAllEmployee")  
  70. </div>  
  71.   
  72. @section Scripts {  
  73.     @Scripts.Render("~/bundles/jqueryval")  
  74. }  
@html.Actionlink("Text","ControllerName")
 
In the above @html.Actionlink, add GetAllEmployee as the controller name.
 
EditEmployee.cshtml 
 
CRUD Operation Using Model View And Controller
  1. @model CRUD.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "EditEmployee";  
  5. }  
  6.   
  7. <h2>@ViewBag.Message</h2>  
  8.   
  9.   
  10. @using (Html.BeginForm())  
  11. {  
  12.     @Html.AntiForgeryToken()  
  13.       
  14.     <div class="form-horizontal">  
  15.         <h4>Employee</h4>  
  16.         <hr />  
  17.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
  18.         @Html.HiddenFor(model => model.ID)  
  19.   
  20.         <div class="form-group">  
  21.             @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })  
  22.             <div class="col-md-10">  
  23.                 @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })  
  24.                 @Html.ValidationMessageFor(model => model.EmpName, ""new { @class = "text-danger" })  
  25.             </div>  
  26.         </div>  
  27.   
  28.         <div class="form-group">  
  29.             @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })  
  30.             <div class="col-md-10">  
  31.                 @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })  
  32.                 @Html.ValidationMessageFor(model => model.Address, ""new { @class = "text-danger" })  
  33.             </div>  
  34.         </div>  
  35.   
  36.         <div class="form-group">  
  37.             @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })  
  38.             <div class="col-md-10">  
  39.                 @Html.DropDownList("Gender"new List<SelectListItem>  
  40.   
  41.                  {  
  42.                     new SelectListItem{ Text="Male", Value = "Male" },  
  43.                     new SelectListItem{ Text="Female", Value = "Female" }  
  44.                  })  
  45.             </div>  
  46.         </div>  
  47.   
  48.         <div class="form-group">  
  49.             @Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label col-md-2" })  
  50.             <div class="col-md-10">  
  51.                 <div class="checkbox">  
  52.                     @Html.EditorFor(model => model.Active)  
  53.                     @Html.ValidationMessageFor(model => model.Active, ""new { @class = "text-danger" })  
  54.                 </div>  
  55.             </div>  
  56.         </div>  
  57.   
  58.         <div class="form-group">  
  59.             <div class="col-md-offset-2 col-md-10">  
  60.                 <input type="submit" value="Save" class="btn btn-default" />  
  61.             </div>  
  62.         </div>  
  63.     </div>  
  64. }  
  65.   
  66. <div>  
  67.     @Html.ActionLink("Back to List""GetAllEmployee")  
  68. </div>  
  69.   
  70. @section Scripts {  
  71.     @Scripts.Render("~/bundles/jqueryval")  
  72. }  
DeleteEmployee.cshtml
 
CRUD Operation Using Model View And Controller
  1. @model CRUD.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "DeleteEmployee";  
  5. }  
  6.   
  7. <h2>DeleteEmployee</h2>  
  8.   
  9. <h3>Are you sure you want to delete this?</h3>  
  10. <div>  
  11.     <h4>Employee</h4>  
  12.     <hr />  
  13.     <dl class="dl-horizontal">  
  14.         <dt>  
  15.             @Html.DisplayNameFor(model => model.EmpName)  
  16.         </dt>  
  17.   
  18.         <dd>  
  19.             @Html.DisplayFor(model => model.EmpName)  
  20.         </dd>  
  21.   
  22.         <dt>  
  23.             @Html.DisplayNameFor(model => model.Address)  
  24.         </dt>  
  25.   
  26.         <dd>  
  27.             @Html.DisplayFor(model => model.Address)  
  28.         </dd>  
  29.   
  30.         <dt>  
  31.             @Html.DisplayNameFor(model => model.Gender)  
  32.         </dt>  
  33.   
  34.         <dd>  
  35.             @Html.DisplayFor(model => model.Gender)  
  36.         </dd>  
  37.         <dt>  
  38.             @Html.DisplayNameFor(model => model.Active)  
  39.         </dt>  
  40.   
  41.         <dd>  
  42.             @Html.DisplayFor(model => model.Active)  
  43.         </dd>  
  44.   
  45.     </dl>  
  46.   
  47.     @using (Html.BeginForm()) {  
  48.         @Html.AntiForgeryToken()  
  49.   
  50.         <div class="form-actions no-color">  
  51.             <input type="submit" value="Delete" class="btn btn-default" /> |  
  52.             @Html.ActionLink("Back to List""GetAllEmployee")  
  53.         </div>  
  54.     }  
  55. </div>   
GetDetails.cshtml
 
CRUD Operation Using Model View And Controller
  1. @model CRUD.Models.Employee  
  2.   
  3. @{  
  4.     ViewBag.Title = "GetDetails";  
  5. }  
  6.   
  7. <h2>GetDetails</h2>  
  8.   
  9. <div>  
  10.     <h4>Employee</h4>  
  11.     <hr />  
  12.     <dl class="dl-horizontal">  
  13.         <dt>  
  14.             @Html.DisplayNameFor(model => model.EmpName)  
  15.         </dt>  
  16.   
  17.         <dd>  
  18.             @Html.DisplayFor(model => model.EmpName)  
  19.         </dd>  
  20.   
  21.         <dt>  
  22.             @Html.DisplayNameFor(model => model.Address)  
  23.         </dt>  
  24.   
  25.         <dd>  
  26.             @Html.DisplayFor(model => model.Address)  
  27.         </dd>  
  28.   
  29.         <dt>  
  30.             @Html.DisplayNameFor(model => model.Gender)  
  31.         </dt>  
  32.   
  33.         <dd>  
  34.             @Html.DisplayFor(model => model.Gender)  
  35.         </dd>  
  36.   
  37.         <dt>  
  38.             @Html.DisplayNameFor(model => model.Active)  
  39.         </dt>  
  40.   
  41.         <dd>  
  42.             @Html.DisplayFor(model => model.Active)  
  43.         </dd>  
  44.   
  45.     </dl>  
  46. </div>  
  47. <p>  
  48.     @Html.ActionLink("Edit""EditEmployee"new { id = Model.ID }) |  
  49.     @Html.ActionLink("Back to List""GetAllEmployee")  
  50. </p>  
GetAllEmployee.cshtml
 
CRUD Operation Using Model View And Controller
  1. @model IEnumerable<CRUD.Models.Employee>  
  2.   
  3. @{  
  4.     ViewBag.Title = "GetAllEmployee";  
  5. }  
  6.   
  7. <h2>@ViewBag.Message</h2>  
  8.   
  9. <p>  
  10.     @Html.ActionLink("Create New""AddEmployee")  
  11. </p>  
  12.   
  13. <table class="table">  
  14.     <tr>  
  15.         <th>  
  16.             @Html.DisplayNameFor(model => model.EmpName)  
  17.         </th>  
  18.         <th>  
  19.             @Html.DisplayNameFor(model => model.Address)  
  20.         </th>  
  21.         <th>  
  22.             @Html.DisplayNameFor(model => model.Gender)  
  23.         </th>  
  24.         <th>  
  25.             @Html.DisplayNameFor(model => model.Active)  
  26.         </th>  
  27.         <th></th>  
  28.     </tr>  
  29.   
  30.     @foreach (var item in Model)  
  31.     {  
  32.         <tr>  
  33.             <td>  
  34.                 @Html.DisplayFor(modelItem => item.EmpName)  
  35.             </td>  
  36.             <td>  
  37.                 @Html.DisplayFor(modelItem => item.Address)  
  38.             </td>  
  39.             <td>  
  40.                 @Html.DisplayFor(modelItem => item.Gender)  
  41.             </td>  
  42.             <td>  
  43.                 @Html.DisplayFor(modelItem => item.Active)  
  44.             </td>  
  45.             <td>  
  46.                 @Html.ActionLink("Edit""EditEmployee"new { id = item.ID }) |  
  47.                 @Html.ActionLink("Details""GetDetails"new { id = item.ID }) |  
  48.                 @Html.ActionLink("Delete""DeleteEmployee"new { id = item.ID })  
  49.             </td>  
  50.         </tr>  
  51.     }  
  52.   
  53. </table>  
After completion of the project and coding above, when you build and run the app, you will see the page looks like the following where you can add, update, and delete records.
CRUD Operation Using Model View And Controller