In this session, we are going to talk about how we can delete multi records in ASP.NET Core and implement this feature in this tutorial web app. During session 48 and session 50 we explain the Delete operation for a single record. Now, we will show you the way that you can delete multi-selected records in a single action.
Delete Multi Records in ASP.NET Core
First, we need to pass the ID of the records that we decide to delete, to the controller. Thus, we need to implement some changes inside the respective View. The View needs a Form tag which will call the Delete method inside the respective Controller. Also, inside the form tag, we can use a table like the practice we have done for the Cost entity. We need to add an input element in each row which name should be the same as the input Parameter of the Delete Action method in the respective Controller. Also, the value of the input element should be the value of the ID of each record. Moreover, we need a button to submit the form.
Then, inside the Controller, we receive the ID of the records we decide to delete. The controller will pass the list to the Repository. Thus we need to add a method to the respective interface and repository to handle this operation.
We implement these changes on this tutorial web app to add multi records delete feature to the web app. You can review the explained code in Category/Index View.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
@model IEnumerable<Category> @{ ViewBag.Title = "Category List"; } <form asp-action="delete" method="post"> <table class="table table-dark mt-3"> <thead> <tr> <th>Select</th> <th>Category Name</th> <th>Description</th> </tr> </thead> <tbody> @foreach (var category in Model) { <tr> <td><input type="checkbox" name="SelectedCatDelete" value="@category.ID"/></td> <td>@category.CategoryName</td> <td>@category.Description</td> </tr> } </tbody> </table> <button type="submit" class="btn btn-danger">Delete Selected Rows</button> </form> |
Also, we add the Index and Delete action methods inside CategoryController to display the category list and handle the delete operation as you can see below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
public class CategoryController : Controller { private readonly ICategoryRepository categoryRepository; public CategoryController(ICategoryRepository _categoryRepository) { categoryRepository = _categoryRepository; } public IActionResult Index() { var CatList = categoryRepository.GetAllCategories(); return View(CatList); } [HttpPost] public IActionResult Delete(IEnumerable<int> SelectedCatDelete) { categoryRepository.Delete(SelectedCatDelete); return RedirectToAction("Index"); } } |
Moreover, we need to add the Delete method inside ICategoryRepository.
1 2 3 4 5 6 7 8 9 |
public interface ICategoryRepository { IEnumerable<Category> GetAllCategories(); IEnumerable<Category> Delete(IEnumerable<int> DeleteList); } |
Now we need to implement this method inside the respective repository.
1 2 3 4 5 6 7 8 9 10 11 |
public IEnumerable<Category> Delete(IEnumerable<int> DeleteList) { var DeleteCatList = webAppDBContext.Categories.Where(z => DeleteList.Contains(z.ID)).ToList(); webAppDBContext.Categories.RemoveRange(DeleteCatList); webAppDBContext.SaveChanges(); return DeleteCatList; } |
We successfully added the multi records delete function to the web app. now we can review the On Delete Action behavior.
On Delete Action
When two or more entities have a relationship with each other, delete and update operations on the parent entity can affect the child entity. For instance, the Category entity is the parent of the Cost entity. The Primary Key (PK) of the Category, which is ID, is the Foreign Key (FK) of CategoryID inside the Cost Entity. That’s why if we decide to delete a category record (for example Bill Category) while the record is used in Cost records (Cost with Bill Category) we should set the behavior of the database regarding the Cost records. Worth mentioning that by default, in this situation the respective records in the Cost entity are deleted when we delete the parent record (On Delete Cascade).
This behavior that we call On Delete Action, can be managed by the database and based on our preferences.
On Delete Action behaviors:
- Cascade: The child record will be deleted if the parent is deleted (default section).
- No Action: The child record stay unchanged if the parent record is deleted.
- SetNull: The value of the property of the child record will be set to null if the parent record is deleted.
- Restricted: The database prevents parent records from deletion while they have child records.
To set this behavior, we need to update the Context class and of course the migration and the database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
public class WebAppDBContext : DbContext { public WebAppDBContext(DbContextOptions<WebAppDBContext> options) : base(options) { } public DbSet<Category> Categories { get; set; } public DbSet<Cost> Costs { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Cost>().HasOne(c => c.Category).WithMany(c => c.Costs).OnDelete(DeleteBehavior.Restrict); } } |
If you need more details, watch this session video. Also, to be updated about our coming sessions, follow us on Instagram, Facebook, Telegram, or YouTube. Moreover, you can have access to the list of all sessions HERE and you can download this session source code from our GitHub.
You can download this Session Slides form HERE.