How To Delete Multiple Rows Using Checkbox In MVC 5

In this article I will demonstrate how to delete multiple rows from database using checkbox in mvc 5. I will have checkbox in table header to check and uncheck all the records in table to delete. I will jquery datatable for searching, sorting and paging.

Introduction

In this article, I will demonstrate how to delete multiple rows from a database using checkbox in MVC 5. I will have a checkbox in the table header to check and uncheck all the records in the table to delete. I will use a jQuery datatable for searching, sorting, and paging.   

Step 1

Open SQL Server 2014 and create a database table and insert some record in it.

  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] [intNULL,  
  7.     [Salary] [nvarchar](50) NULL,  
  8.  CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED   
  9. (  
  10.     [ID] ASC  
  11. )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ONON [PRIMARY]  
  12. ON [PRIMARY]  
  13.   
  14. 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, 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 MvcDeleteMultipleRows_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 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 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 dot (.). Choose your database and click on OK.

Screenshot for adding entity framework 4

ASP.NET

Connection will be added. If you wish 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 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

ASP.NET

Following class will be added, 

  1. namespace MvcDeleteMultipleRows_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 4

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

ASP.NET

After clicking on controller a window will appear 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 System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using MvcDeleteMultipleRows_Demo.Models;  
  7.   
  8. namespace MvcDeleteMultipleRows_Demo.Controllers  
  9. {  
  10.     public class HomeController : Controller  
  11.     {  
  12.         private DBModel db = new DBModel();  
  13.         [HttpGet]  
  14.         public ActionResult Index()  
  15.         {  
  16.             ViewBag.ListEmployee = this.db.Employees.ToList();  
  17.             return View();  
  18.         }  
  19.   
  20.         [HttpPost]  
  21.         public ActionResult Index(FormCollection formCollection)  
  22.         {  
  23.             string[] ids = formCollection["ID"].Split(new char[]{ ',' });  
  24.             foreach (string id in ids)  
  25.             {  
  26.                 var employee = this.db.Employees.Find(int.Parse(id));  
  27.                 this.db.Employees.Remove(employee);  
  28.                 this.db.SaveChanges();  
  29.             }  
  30.             return RedirectToAction("Index");  
  31.         }  
  32.     }  
  33. }  

Step 5

Right click on index action method in 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

Click on Tools, select NuGet Package Manager, then choose Manage NuGet Packages for Solution click on it.

Screenshot for NuGet Package

ASP.NET

After that a window will appear choose Browse type bootstrap and install package in project. Similarly type JQuery and install latest version of JQuery package in project and jquery validation file from NuGet then close NuGet Solution. Keep required bootstrap and jQuery files and delete remaining files if not using, or you can download  and add in project.

ASP.NET

Step 7

Add required script and style in head section of view. 

  1. <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">  
  2.     <link href="~/Content/bootstrap.min.css" rel="stylesheet" />  
  3.     <script src="~/scripts/jquery-3.3.1.min.js"></script>  
  4.     <script src="~/scripts/bootstrap.min.js"></script>  
  5.     <link href="~/Content/dataTables.bootstrap4.min.css" rel="stylesheet" />  
  6.     <script src="~/scripts/jquery.dataTables.min.js"></script>  
  7.     <script src="~/scripts/dataTables.bootstrap4.min.js"></script>  
  8.     <script type="text/javascript">  
  9.         $(document).ready(function () {  
  10.             $('#DataTable').DataTable();  
  11.             $('#checkBoxAll').click(function () {  
  12.                 if ($(this).is(":checked")) {  
  13.                     $(".chkCheckBoxId").prop("checked"true)  
  14.                 }  
  15.                 else {  
  16.                     $(".chkCheckBoxId").prop("checked"false)  
  17.                 }  
  18.             });  
  19.         });  
  20.     </script>  

Step 8

Design view with HTML, cshtml and bootstrap 4 classes,

  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 rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">  
  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.     <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.             $('#checkBoxAll').click(function () {  
  22.                 if ($(this).is(":checked")) {  
  23.                     $(".chkCheckBoxId").prop("checked"true)  
  24.                 }  
  25.                 else {  
  26.                     $(".chkCheckBoxId").prop("checked"false)  
  27.                 }  
  28.             });  
  29.         });  
  30.     </script>  
  31. </head>  
  32. <body>  
  33.     <div class="container py-4">  
  34.         <div class="card">  
  35.             <div class="card-header bg-primary text-white">  
  36.                 <h5>Employee List</h5>  
  37.             </div>  
  38.             <div class="card-body">  
  39.                 @using (Html.BeginForm("Index""Home", FormMethod.Post))  
  40.                 {  
  41.                     <button type="submit" value="Delete" class="btn btn-sm btn-danger rounded-0" onclick="return confirm('Are you sure?')"><i class="fa fa-trash-o"></i> Delete</button>  
  42.                     <br /><br />  
  43.                     <table id="DataTable" style="width:100%;" class="table table-bordered">  
  44.                         <thead>  
  45.                             <tr>  
  46.                                 <th><input type="checkbox" id="checkBoxAll" class="custom-checkbox" /></th>  
  47.                                 <th>EMP ID</th>  
  48.                                 <th>Name</th>  
  49.                                 <th>Position</th>  
  50.                                 <th>Office</th>  
  51.                                 <th>Age</th>  
  52.                                 <th>Salary</th>  
  53.                             </tr>  
  54.                         </thead>  
  55.                         <tbody>  
  56.                             @foreach (var employee in ViewBag.ListEmployee)  
  57.                              {  
  58.                                 <tr>  
  59.                                     <td><input type="checkbox" name="ID" value="@employee.ID" class="custom-checkbox chkCheckBoxId" /></td>  
  60.                                     <td>@employee.ID</td>  
  61.                                     <td>@employee.Name</td>  
  62.                                     <td>@employee.Position</td>  
  63.                                     <td>@employee.Office</td>  
  64.                                     <td>@employee.Age</td>  
  65.                                     <td>@employee.Salary</td>  
  66.                                 </tr>  
  67.                             }  
  68.                         </tbody>  
  69.                     </table>  
  70.                 }  
  71.             </div>  
  72.         </div>  
  73.     </div>  
  74. </body>  
  75. </html>  

Step 9

Run Project ctrl+F5

Screenshot 1

ASP.NET

Screenshot 2

ASP.NET

Screenshot 3

ASP.NET