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] [bit] NULL,
  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 = ON) ON [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. CREATE PROCEDURE usp_GetAllEMployee
  12. AS
  13. BEGIN
  14. SELECT ID, EmpName,Address,Gender,Active from t_Employee
  15. END
  16. CREATE PROCEDURE usp_UpdateEmployee
  17. @ID int,
  18. @EmpName NVARCHAR(50),
  19. @Address NVARCHAR(50),
  20. @Gender NVARCHAR(50),
  21. @Active bit
  22. AS
  23. BEGIN
  24. UPDATE t_Employee
  25. SET EmpName=@EmpName,
  26. Address= @Address,
  27. Gender= @Gender,
  28. Active= @Active
  29. WHERE ID=@ID
  30. END
  31. CREATE PROCEDURE usp_DeleteEMployee
  32. @ID int
  33. AS
  34. BEGIN
  35. Delete From t_Employee
  36. WHERE ID=@ID
  37. 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 { get; set; }
  5. [Required(ErrorMessage = "Please Enter Employee Name")]
  6. public string EmpName { get; set; }
  7. [Required(ErrorMessage = "Please Enter Employee Address")]
  8. public string Address { get; set; }
  9. [Required(ErrorMessage = "Please Enter Gender")]
  10. public string Gender { get; set; }
  11. public bool Active { get; set; }
  12. }
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. public ActionResult AddEmployee()
  4. {
  5. try
  6. {
  7. return View();
  8. }
  9. catch (Exception ex)
  10. {
  11. throw ex;
  12. }
  13. }
  14. [HttpPost]
  15. public ActionResult AddEmployee(Employee Emp)
  16. {
  17. try
  18. {
  19. if (ModelState.IsValid)
  20. {
  21. EmployeeDbContext emprep = new EmployeeDbContext();
  22. if (emprep.AddEmployee(Emp))
  23. {
  24. ViewBag.Message = "Record Saved Successfully";
  25. }
  26. }
  27. return RedirectToAction("GetAllEmployee");
  28. }
  29. catch (Exception ex)
  30. {
  31. throw ex;
  32. }
  33. }
  34. public ActionResult EditEmployee(int id)
  35. {
  36. try
  37. {
  38. EmployeeDbContext emprep = new EmployeeDbContext();
  39. return View(emprep.GetAllEmployee().Find(Emp => Emp.ID == id));
  40. }
  41. catch (Exception ex)
  42. {
  43. throw ex;
  44. }
  45. }
  46. [HttpPost]
  47. public ActionResult EditEmployee(int id, Employee Emp)
  48. {
  49. try
  50. {
  51. if (ModelState.IsValid)
  52. {
  53. EmployeeDbContext emprep = new EmployeeDbContext();
  54. if (emprep.EditEmployee(id, Emp))
  55. {
  56. ViewBag.Message = "Record Updated Successfully";
  57. }
  58. }
  59. return RedirectToAction("GetAllEmployee");
  60. }
  61. catch (Exception ex)
  62. {
  63. throw ex;
  64. }
  65. }
  66. public ActionResult GetAllEmployee(Employee Emp)
  67. {
  68. try
  69. {
  70. EmployeeDbContext emprep = new EmployeeDbContext();
  71. ModelState.Clear();
  72. return View(emprep.GetAllEmployee());
  73. }
  74. catch (Exception ex)
  75. {
  76. throw ex;
  77. }
  78. }
  79. public ActionResult GetDetails(int id)
  80. {
  81. try
  82. {
  83. EmployeeDbContext emprep = new EmployeeDbContext();
  84. return View(emprep.GetAllEmployee().Find(Emp => Emp.ID == id));
  85. }
  86. catch (Exception ex)
  87. {
  88. throw ex;
  89. }
  90. }
  91. public ActionResult DeleteEmployee(int? id)
  92. {
  93. try
  94. {
  95. EmployeeDbContext Emprep = new EmployeeDbContext();
  96. return View(Emprep.GetAllEmployee().Find(x => x.ID == id));
  97. }
  98. catch (Exception ex)
  99. {
  100. throw ex;
  101. }
  102. }
  103. [HttpPost]
  104. public ActionResult DeleteEmployee(int id)
  105. {
  106. try
  107. {
  108. if (ModelState.IsValid)
  109. {
  110. EmployeeDbContext emprep = new EmployeeDbContext();
  111. if (emprep.DeleteEmployee(id))
  112. {
  113. ViewBag.Message = "Record Deleted Successfully";
  114. }
  115. }
  116. return RedirectToAction("GetAllEmployee");
  117. }
  118. catch (Exception ex)
  119. {
  120. throw ex;
  121. }
  122. }
  123. }
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. public List<Employee> GetAllEmployee()
  16. {
  17. DataTable dt = new DataTable();
  18. List<Employee> emp = new List<Employee>();
  19. try
  20. {
  21. using (var con = new SqlConnection(constr))
  22. {
  23. using (var cmd = new SqlCommand("usp_GetAllEMployee", con))
  24. {
  25. cmd.CommandType = System.Data.CommandType.StoredProcedure;
  26. SqlDataAdapter ds = new SqlDataAdapter(cmd);
  27. con.Open();
  28. ds.Fill(dt);
  29. foreach (DataRow dr in dt.Rows)
  30. {
  31. emp.Add(
  32. new Employee
  33. {
  34. ID = Convert.ToInt32(dr["ID"]),
  35. EmpName = Convert.ToString(dr["EMPName"]),
  36. Address = Convert.ToString(dr["Address"]),
  37. Gender = Convert.ToString(dr["Gender"]),
  38. Active = Convert.ToBoolean(dr["Active"])
  39. }
  40. );
  41. };
  42. }
  43. }
  44. return emp;
  45. }
  46. catch (Exception ex)
  47. {
  48. throw ex;
  49. }
  50. }
  51. public bool AddEmployee(Employee Emp)
  52. {
  53. bool result = false;
  54. try
  55. {
  56. using (var con = new SqlConnection(constr))
  57. {
  58. using (var cmd = new SqlCommand("usp_AddEmployee", con))
  59. {
  60. cmd.CommandType = CommandType.StoredProcedure;
  61. cmd.Parameters.AddWithValue("@EmpName", Emp.EmpName);
  62. cmd.Parameters.AddWithValue("@Address", Emp.Address);
  63. cmd.Parameters.AddWithValue("@Gender", Emp.Gender);
  64. cmd.Parameters.AddWithValue("@Active", Emp.Active);
  65. con.Open();
  66. int i = cmd.ExecuteNonQuery();
  67. con.Close();
  68. if (i > 0)
  69. {
  70. result = true;
  71. }
  72. else
  73. {
  74. result = false;
  75. }
  76. }
  77. }
  78. return result;
  79. }
  80. catch (Exception ex)
  81. {
  82. throw ex;
  83. }
  84. }
  85. public bool EditEmployee(int id, Employee Emp)
  86. {
  87. bool result = false;
  88. try
  89. {
  90. using (var con = new SqlConnection(constr))
  91. {
  92. using (var cmd = new SqlCommand("usp_UpdateEmployee", con))
  93. {
  94. cmd.CommandType = CommandType.StoredProcedure;
  95. cmd.Parameters.AddWithValue("@ID", id);
  96. cmd.Parameters.AddWithValue("@EmpName", Emp.EmpName);
  97. cmd.Parameters.AddWithValue("@Address", Emp.Address);
  98. cmd.Parameters.AddWithValue("@Gender", Emp.Gender);
  99. cmd.Parameters.AddWithValue("@Active", Emp.Active);
  100. con.Open();
  101. int i = cmd.ExecuteNonQuery();
  102. if (i > 0)
  103. {
  104. result = true;
  105. }
  106. else
  107. {
  108. result = false;
  109. }
  110. }
  111. }
  112. return result;
  113. }
  114. catch (Exception ex)
  115. {
  116. throw ex;
  117. }
  118. }
  119. public bool DeleteEmployee(int id)
  120. {
  121. bool result = false;
  122. try
  123. {
  124. using (var con = new SqlConnection(constr))
  125. {
  126. using (var cmd = new SqlCommand("usp_DeleteEMployee", con))
  127. {
  128. cmd.CommandType = CommandType.StoredProcedure;
  129. cmd.Parameters.AddWithValue("@ID", id);
  130. con.Open();
  131. int i = cmd.ExecuteNonQuery();
  132. if (i > 0)
  133. {
  134. result = true;
  135. }
  136. else
  137. {
  138. result = false;
  139. }
  140. }
  141. }
  142. return result;
  143. }
  144. catch (Exception ex)
  145. {
  146. throw ex;
  147. }
  148. }
  149. public DataSet GetDetails()
  150. {
  151. DataSet ds = new DataSet();
  152. try
  153. {
  154. using (var conn = new SqlConnection(constr))
  155. {
  156. using (var cmd = new SqlCommand("usp_Get_Employee", conn))
  157. {
  158. cmd.CommandType = CommandType.StoredProcedure;
  159. conn.Open();
  160. SqlDataAdapter dt = new SqlDataAdapter(cmd);
  161. dt.Fill(ds);
  162. conn.Close();
  163. }
  164. }
  165. return ds;
  166. }
  167. catch (Exception ex)
  168. {
  169. throw ex;
  170. }
  171. }
  172. }
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. ViewBag.Title = "AddEmployee";
  4. }
  5. <h2>@ViewBag.Message</h2>
  6. @using (Html.BeginForm())
  7. {
  8. @Html.AntiForgeryToken()
  9. <div class="form-horizontal">
  10. <h4>Employee</h4>
  11. <hr />
  12. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  13. <div class="form-group">
  14. @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })
  15. <div class="col-md-10">
  16. @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })
  17. @Html.ValidationMessageFor(model => model.EmpName, "", new { @class = "text-danger" })
  18. </div>
  19. </div>
  20. <div class="form-group">
  21. @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
  22. <div class="col-md-10">
  23. @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
  24. @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
  25. </div>
  26. </div>
  27. <div class="form-group">
  28. @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
  29. <div class="col-md-10">
  30. @Html.DropDownList("Gender", new List<SelectListItem>
  31. {
  32. new SelectListItem{ Text="---Select Gender---", Value = "Select Gender" },
  33. new SelectListItem{ Text="Male", Value = "Male" },
  34. new SelectListItem{ Text="Female", Value = "Female" }
  35. }, new { @class = "textbox" })
  36. </div>
  37. </div>
  38. <div class="form-group">
  39. @Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label col-md-2" })
  40. <div class="col-md-10">
  41. <div class="checkbox">
  42. @Html.EditorFor(model => model.Active)
  43. @Html.ValidationMessageFor(model => model.Active, "", new { @class = "text-danger" })
  44. </div>
  45. </div>
  46. </div>
  47. <div class="form-group">
  48. <div class="col-md-offset-2 col-md-10">
  49. <input type="submit" value="Create" class="btn btn-default" />
  50. </div>
  51. </div>
  52. </div>
  53. }
  54. <div>
  55. @Html.ActionLink("Back to List", "GetAllEmployee")
  56. </div>
  57. @section Scripts {
  58. @Scripts.Render("~/bundles/jqueryval")
  59. }
@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. ViewBag.Title = "EditEmployee";
  4. }
  5. <h2>@ViewBag.Message</h2>
  6. @using (Html.BeginForm())
  7. {
  8. @Html.AntiForgeryToken()
  9. <div class="form-horizontal">
  10. <h4>Employee</h4>
  11. <hr />
  12. @Html.ValidationSummary(true, "", new { @class = "text-danger" })
  13. @Html.HiddenFor(model => model.ID)
  14. <div class="form-group">
  15. @Html.LabelFor(model => model.EmpName, htmlAttributes: new { @class = "control-label col-md-2" })
  16. <div class="col-md-10">
  17. @Html.EditorFor(model => model.EmpName, new { htmlAttributes = new { @class = "form-control" } })
  18. @Html.ValidationMessageFor(model => model.EmpName, "", new { @class = "text-danger" })
  19. </div>
  20. </div>
  21. <div class="form-group">
  22. @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })
  23. <div class="col-md-10">
  24. @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })
  25. @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })
  26. </div>
  27. </div>
  28. <div class="form-group">
  29. @Html.LabelFor(model => model.Gender, htmlAttributes: new { @class = "control-label col-md-2" })
  30. <div class="col-md-10">
  31. @Html.DropDownList("Gender", new List<SelectListItem>
  32. {
  33. new SelectListItem{ Text="Male", Value = "Male" },
  34. new SelectListItem{ Text="Female", Value = "Female" }
  35. })
  36. </div>
  37. </div>
  38. <div class="form-group">
  39. @Html.LabelFor(model => model.Active, htmlAttributes: new { @class = "control-label col-md-2" })
  40. <div class="col-md-10">
  41. <div class="checkbox">
  42. @Html.EditorFor(model => model.Active)
  43. @Html.ValidationMessageFor(model => model.Active, "", new { @class = "text-danger" })
  44. </div>
  45. </div>
  46. </div>
  47. <div class="form-group">
  48. <div class="col-md-offset-2 col-md-10">
  49. <input type="submit" value="Save" class="btn btn-default" />
  50. </div>
  51. </div>
  52. </div>
  53. }
  54. <div>
  55. @Html.ActionLink("Back to List", "GetAllEmployee")
  56. </div>
  57. @section Scripts {
  58. @Scripts.Render("~/bundles/jqueryval")
  59. }
DeleteEmployee.cshtml
CRUD Operation Using Model View And Controller
  1. @model CRUD.Models.Employee
  2. @{
  3. ViewBag.Title = "DeleteEmployee";
  4. }
  5. <h2>DeleteEmployee</h2>
  6. <h3>Are you sure you want to delete this?</h3>
  7. <div>
  8. <h4>Employee</h4>
  9. <hr />
  10. <dl class="dl-horizontal">
  11. <dt>
  12. @Html.DisplayNameFor(model => model.EmpName)
  13. </dt>
  14. <dd>
  15. @Html.DisplayFor(model => model.EmpName)
  16. </dd>
  17. <dt>
  18. @Html.DisplayNameFor(model => model.Address)
  19. </dt>
  20. <dd>
  21. @Html.DisplayFor(model => model.Address)
  22. </dd>
  23. <dt>
  24. @Html.DisplayNameFor(model => model.Gender)
  25. </dt>
  26. <dd>
  27. @Html.DisplayFor(model => model.Gender)
  28. </dd>
  29. <dt>
  30. @Html.DisplayNameFor(model => model.Active)
  31. </dt>
  32. <dd>
  33. @Html.DisplayFor(model => model.Active)
  34. </dd>
  35. </dl>
  36. @using (Html.BeginForm()) {
  37. @Html.AntiForgeryToken()
  38. <div class="form-actions no-color">
  39. <input type="submit" value="Delete" class="btn btn-default" /> |
  40. @Html.ActionLink("Back to List", "GetAllEmployee")
  41. </div>
  42. }
  43. </div>
GetDetails.cshtml
CRUD Operation Using Model View And Controller
  1. @model CRUD.Models.Employee
  2. @{
  3. ViewBag.Title = "GetDetails";
  4. }
  5. <h2>GetDetails</h2>
  6. <div>
  7. <h4>Employee</h4>
  8. <hr />
  9. <dl class="dl-horizontal">
  10. <dt>
  11. @Html.DisplayNameFor(model => model.EmpName)
  12. </dt>
  13. <dd>
  14. @Html.DisplayFor(model => model.EmpName)
  15. </dd>
  16. <dt>
  17. @Html.DisplayNameFor(model => model.Address)
  18. </dt>
  19. <dd>
  20. @Html.DisplayFor(model => model.Address)
  21. </dd>
  22. <dt>
  23. @Html.DisplayNameFor(model => model.Gender)
  24. </dt>
  25. <dd>
  26. @Html.DisplayFor(model => model.Gender)
  27. </dd>
  28. <dt>
  29. @Html.DisplayNameFor(model => model.Active)
  30. </dt>
  31. <dd>
  32. @Html.DisplayFor(model => model.Active)
  33. </dd>
  34. </dl>
  35. </div>
  36. <p>
  37. @Html.ActionLink("Edit", "EditEmployee", new { id = Model.ID }) |
  38. @Html.ActionLink("Back to List", "GetAllEmployee")
  39. </p>
GetAllEmployee.cshtml
CRUD Operation Using Model View And Controller
  1. @model IEnumerable<CRUD.Models.Employee>
  2. @{
  3. ViewBag.Title = "GetAllEmployee";
  4. }
  5. <h2>@ViewBag.Message</h2>
  6. <p>
  7. @Html.ActionLink("Create New", "AddEmployee")
  8. </p>
  9. <table class="table">
  10. <tr>
  11. <th>
  12. @Html.DisplayNameFor(model => model.EmpName)
  13. </th>
  14. <th>
  15. @Html.DisplayNameFor(model => model.Address)
  16. </th>
  17. <th>
  18. @Html.DisplayNameFor(model => model.Gender)
  19. </th>
  20. <th>
  21. @Html.DisplayNameFor(model => model.Active)
  22. </th>
  23. <th></th>
  24. </tr>
  25. @foreach (var item in Model)
  26. {
  27. <tr>
  28. <td>
  29. @Html.DisplayFor(modelItem => item.EmpName)
  30. </td>
  31. <td>
  32. @Html.DisplayFor(modelItem => item.Address)
  33. </td>
  34. <td>
  35. @Html.DisplayFor(modelItem => item.Gender)
  36. </td>
  37. <td>
  38. @Html.DisplayFor(modelItem => item.Active)
  39. </td>
  40. <td>
  41. @Html.ActionLink("Edit", "EditEmployee", new { id = item.ID }) |
  42. @Html.ActionLink("Details", "GetDetails", new { id = item.ID }) |
  43. @Html.ActionLink("Delete", "DeleteEmployee", new { id = item.ID })
  44. </td>
  45. </tr>
  46. }
  47. </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