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.
- CREATE TABLE [dbo].[Employees](
- [ID] [int] IDENTITY(1,1) NOT NULL,
- [Name] [nvarchar](50) NULL,
- [Position] [nvarchar](50) NULL,
- [Office] [nvarchar](50) NULL,
- [Age] [int] NULL,
- [Salary] [nvarchar](50) NULL,
- CONSTRAINT [PK_Employees] PRIMARY KEY CLUSTERED
- (
- [ID] ASC
- )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
- ) ON [PRIMARY]
- GO
Step 2
Open Visual Studio 2015, click on New Project, and create an empty web application project.
Screenshot for creating new project 1

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

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

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

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

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

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

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

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

Screenshot for adding entity framework-7

Entity framework will be added and respective class gets generated under Models folder.
Screenshot for adding entity framework 8

Following class will be added,
- namespace MvcDeleteMultipleRows_Demo.Models
- {
- using System;
- using System.Collections.Generic;
- public partial class Employee
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public string Position { get; set; }
- public string Office { get; set; }
- public Nullable<int> Age { get; set; }
- public string Salary { get; set; }
- }
- }
Step 4
Right click on Controllers folder, select Add, then choose Controller as shown in below screenshot.

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

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.

Complete code for controller
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcDeleteMultipleRows_Demo.Models;
- namespace MvcDeleteMultipleRows_Demo.Controllers
- {
- public class HomeController : Controller
- {
- private DBModel db = new DBModel();
- [HttpGet]
- public ActionResult Index()
- {
- ViewBag.ListEmployee = this.db.Employees.ToList();
- return View();
- }
- [HttpPost]
- public ActionResult Index(FormCollection formCollection)
- {
- string[] ids = formCollection["ID"].Split(new char[]{ ',' });
- foreach (string id in ids)
- {
- var employee = this.db.Employees.Find(int.Parse(id));
- this.db.Employees.Remove(employee);
- this.db.SaveChanges();
- }
- return RedirectToAction("Index");
- }
- }
- }







smita sPosted Mar 19, 2020, 2:40 PM
Are you submitting entire table rows to action method or only those records which checked by checkbox? i have requirment to submit those records which is checked , how i can do it
Madan GhimirePosted Feb 10, 2020, 2:29 AM
Unable to pass data to FormCollection formCollection on Delete Post method Object reference not set to an instance of an object.
xueling liPosted Jul 31, 2019, 3:39 AM
Thanks!I SUCCESS!
Shyam RaghuvanshiPosted Mar 9, 2019, 10:01 AM
Can you please provide complete source sample code.
Adil HassanPosted Sep 26, 2018, 8:32 AM
Hello I keep getting System.NullReferenceException: Object reference not set to an instance of an object. for this line of code : string[] ids = formCollection["ID"].Split(new char[]{ ',' });
Huy DangPosted Sep 9, 2018, 11:49 AM
I love it.
Satyaprakash SamantarayPosted Jul 4, 2018, 8:56 AM
Nice article and very clear description