Delete Multi Records in ASP.NET Core – Session 56

Multi Record Deleting & On Delete Action - Session56

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.

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.

Moreover, we need to add the Delete method inside ICategoryRepository.

Now we need to implement this method inside the respective repository.

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.

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.

0.00 avg. rating (0% score) - 0 votes


0 0 votes
Article Rating

Inline Feedbacks
View all comments