In this article I will explain with an example, how to delete multiple records using Entity Framework in ASP.Net MVC.
Note: For beginners in ASP.Net MVC and Entity Framework, please refer my article ASP.Net MVC: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework.
 
 

Database

I have made use of the following table Customers with the schema as follow.
ASP.Net MVC: Delete multiple records using Entity Framework
 
I have already inserted few records in the table.
ASP.Net MVC: Delete multiple records using Entity Framework
 
Note: You can download the database table SQL by clicking the download link below.
            Download SQL file
 
 

Entity Framework Model

Once the Entity Framework is configured and connected to the database table, the Model will look as shown below.
ASP.Net MVC: Delete multiple records using Entity Framework
 
 

Controller

The Controller consists of following Action methods.

Action method for handling GET operation

Inside this Action method, simply the View is returned.
 

Action method for handling POST operation

Inside the Action Method, CustomerIds to be deleted are fetched from the TextBox and Converts them into an Integer Array.
Then based on the Array, Customers are referenced and passed to the RemoveRange method of Entity Framework and the SaveChanges method of Entity Framework is called which updates the changes to the database.
Then, a success message is set to a TempData object named “Message”.
Finally, the page is redirected to Index Action method.
public class HomeController  : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult Delete(string ids)
    {
        AjaxSamplesEntities entities = new AjaxSamplesEntities();
        int rowsAffected = 0;
        int[]customerIds Array.ConvertAll(ids.Split(','), Int32.Parse);
 
        // Referencing Customers.
        List<Customer> deletedCustomers entities.Customers.Where(customer => customerIds.Contains(customer.CustomerId)).ToList();
 
        // Deleting Customers.
        rowsAffected entities.Customers.RemoveRange(deletedCustomers).Count();
        entities.SaveChanges();
        TempData["Message"] = "Customer records are deleted.";
        return RedirectToAction("Index");
    }
}
 
 

View

HTML Markup

The View consists of HTML Form which has been created using the Html.BeginForm method with the following parameters.
asp-action – Name of the Action. In this case the name is Delete.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
The form consists of text and submit button.
The TempData object named Message is checked for NULL and if it is not NULL then the value of the object is displayed using JavaScript Alert MessageBox.
@{
     Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    @using (Html.BeginForm("Delete", "Home", FormMethod.Post))
    {
        <input type="text" id="txtId" name="ids" /> 
        <input type="submit" id="btnSubmit" value="Delete"/> 
    }
    @if (TempData["Message"] != null)
    {
        <script type="text/javascript">
            window.onload = function () {
               
                alert('@TempData["Message"] ');
        };
        </script>
    }
</body>
</html>
 
 

Screenshot

ASP.Net MVC: Delete multiple records using Entity Framework
 

Records After Deleting

ASP.Net MVC: Delete multiple records using Entity Framework
 
 

Downloads