Performing Update and Delete Operations in ASP.NET Core 3 Razor Pages Using Microsoft SQL Server

Introduction

 
This article is the continuation of the article “Inserting Data Into SQL Server Database Using ASP.NET Core 3 Razor Pages” where we have implemented the create razor page to insert a record inside the database. In this article, we will be working on editing and deleting a razor page to perform edit and delete functionality of the crud operations on the BookList objects in ASP.Net Core Razor Pages.
 
Creation of Edit razor page
 
Now we will create a razor page called Edit to edit and update the already-created data inside the SQL server database.
 
Performing Update And Delete Operations In ASP.NET Core 3 Razor Pages Using Microsoft SQL Server
 
For that to happen, we need to make changes inside the Ind.cshtml file. Inside the edit button anchor tag, we need to pass the routing that is the asp-page will be an Edit page that we will be creating soon.
  1. @page  
  2. @model BookList.IndModel  
  3. <br />  
  4. <div class="container row p-0 m-0">  
  5.     <div class="col-10">  
  6.         <h2 class="text-info">Book List</h2>  
  7.     </div>  
  8.     <div class="col-2">  
  9.         <a asp-page="Create" class="btn btn-info form-control text-white">Create New Book</a>  
  10.     </div>  
  11.     <div class="col-12 border p-3 mt-3">  
  12.         <form method="post">  
  13.             @if(Model.Books.Count()>0)  
  14.             {  
  15.                 <table class="table table-striped border">  
  16.                     <tr class="table-secondary">  
  17.                         <th>  
  18.                             <label asp-for="Books.FirstOrDefault().Name"></label>  
  19.                         </th>  
  20.                         <th>  
  21.                             <label asp-for="Books.FirstOrDefault().Author"></label>  
  22.                         </th>  
  23.                         <th>  
  24.                             <label asp-for="Books.FirstOrDefault().ISBN"></label>  
  25.                         </th>  
  26.                         <th>  
  27.   
  28.                         </th>  
  29.                     </tr>  
  30.                     @foreach(var item in Model.Books)  
  31.                     {  
  32.                 <tr>  
  33.                     <td>  
  34.                         @Html.DisplayFor(m => item.Name)  
  35.                     </td>  
  36.                     <td>  
  37.                         @Html.DisplayFor(m => item.Author)  
  38.                     </td>  
  39.                     <td>  
  40.                         @Html.DisplayFor(m => item.ISBN)  
  41.                     </td>  
  42.                     <td>  
  43.                         <button asp-page-handler="Delete" asp-route-id="@item.Id" onclick="return confirm('Are you sure you want to delete?')" class="btn btn-danger btn-sm">Delete</button>  
  44.                         <a asp-page="Edit" asp-route-id="@item.Id" class="btn btn-success btn-sm text-white">Edit</a>  
  45.                     </td>  
  46.                 </tr>  
  47.                     }  
  48.                 </table>   
  49.             }  
  50.             else  
  51.             {  
  52.                 <p>No Books Available.</p>  
  53.             }  
  54.         </form>  
  55.     </div>  
  56. </div>  
Whenever the user clicks on the edit button, we want to pass the ID of the field that they are editing. In this case that would be the ID of the book. So in order to pass that, we will be using another tag helper that is asp route and the name of the property that we want to pass.
 
Here we want to pass the ID of the book. This way when a user clicks on edit, it will go the edit razor page and it will also pass the ID as a parameter.
 
So now we have to create a new razor page named Edit where we will be defining the edit functionality as well as a user interface to retrieve book data for editing and updating it according to the user's needs.
  1. @page  
  2. @model BookList.EditModel  
  3.   
  4. <br />  
  5. <h2 class="text-info">Edit Book</h2>  
  6. <br />  
  7.   
  8. <div class="border container" style="padding:30px;">  
  9.     <form method="post">  
  10.         <input type="hidden" asp-for="Book.Id" />   
  11.         <div class="form-group row">  
  12.             <div class="col-4">  
  13.                 <label asp-for="Book.Name"></label>  
  14.             </div>  
  15.             <div class="col-6">  
  16.                 <input asp-for="Book.Name" class="form-control" />  
  17.             </div>  
  18.         </div>  
  19.   
  20.         <div class="form-group row">  
  21.             <div class="col-4">  
  22.                 <label asp-for="Book.Author"></label>  
  23.             </div>  
  24.             <div class="col-6">  
  25.                 <input asp-for="Book.Author" class="form-control" />  
  26.             </div>  
  27.         </div>  
  28.   
  29.         <div class="form-group row">  
  30.             <div class="col-4">  
  31.                 <label asp-for="Book.ISBN"></label>  
  32.             </div>  
  33.             <div class="col-6">  
  34.                 <input asp-for="Book.ISBN" class="form-control" />  
  35.             </div>  
  36.         </div>  
  37.         <div class="form-group row">  
  38.             <div class="col-3 offset-4">  
  39.                 <input type="submit" value="Update" class="btn btn-primary form-control" />  
  40.             </div>  
  41.             <div class="col-3">  
  42.                 <a asp-page="Ind" class="btn btn-success form-control">Back to List</a>  
  43.             </div>  
  44.         </div>  
  45.     </form>  
  46. </div>  
Inside the Edit.cshtml.cs file, we are creating a get handler which will get a parameter called Id that we just pass. It will be of integer type and based on this, we will be retrieve the book.
 
We have to use Application Db Context to interact with database using entity framework because we will be updating the data related to Book object.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Threading.Tasks;  
  5. using BookList.Model;  
  6. using Microsoft.AspNetCore.Mvc;  
  7. using Microsoft.AspNetCore.Mvc.RazorPages;  
  8.   
  9. namespace BookList  
  10. {  
  11.     public class EditModel : PageModel  
  12.     {  
  13.         private ApplicationDbContext _db;  
  14.   
  15.         public EditModel(ApplicationDbContext db)  
  16.         {  
  17.             _db = db;  
  18.         }  
  19.   
  20.         [BindProperty]  
  21.         public Book Book { getset; }  
  22.         public async Task OnGet(int id)  
  23.         {  
  24.             Book = await _db.Book.FindAsync(id);  
  25.         }  
  26.   
  27.         public async Task<IActionResult> OnPost()  
  28.         {  
  29.             if (ModelState.IsValid)  
  30.             {  
  31.                 var BookFromDb = await _db.Book.FindAsync(Book.Id);  
  32.                 BookFromDb.Name=Book.Name;  
  33.                 BookFromDb.ISBN = Book.ISBN;  
  34.                 BookFromDb.Author=Book.Author;  
  35.   
  36.                 await _db.SaveChangesAsync();  
  37.                 return RedirectToPage("Ind");  
  38.                   
  39.             }  
  40.             return RedirectToPage();  
  41.         }  
  42.     }  
  43. }  
With the help of OnGet handler method, we are populating the book object with the data that we received from database. FindAsync is a method available inside entity framework.
 
Now we will design an edit view and will demonstrate the functionality of editing and updating the properties of book object inside the database.
 
Now we will edit the book UI. The user interface will be similar to the create user interface that has been created during previous articles. The only difference is that here we will be loading the data from the SQL server database.
 
Performing Update And Delete Operations In ASP.NET Core 3 Razor Pages Using Microsoft SQL Server
In the output inside URL, the id of the particular book has also been passed because we used an asp-route-id custom tag helper.
 
Creation of Edit post handler
 
We have to add a post handler to post the updated data back to the database. In the post handler, we will be redirected to a page that is why the return type of OnPost handler is IAcionResult.
  1. public async Task<IActionResult> OnPost()  
  2. {  
  3.     if (ModelState.IsValid)  
  4.     {  
  5.         var BookFromDb = await _db.Book.FindAsync(Book.Id);  
  6.         BookFromDb.Name=Book.Name;  
  7.         BookFromDb.ISBN = Book.ISBN;  
  8.         BookFromDb.Author=Book.Author;  
  9.   
  10.         await _db.SaveChangesAsync();  
  11.         return RedirectToPage("Ind");  
  12.                   
  13.         }  
  14.         return RedirectToPage();  
Here we are checking if the model state is valid or not. If it is valid then we will retrieve a book from the database as we will be editing the data related to a particular book.
 
After updating the book, we will be redirected to the index page. If the model state is not valid then we will redirect and return back to the page.
 
Now in order for the edit function to work, id property is needed because books will be retrieved based on their id values. For that, we are defining an input text field of type hidden and will use asp-for tag helper for the Id property. So now the edit functionality has been implemented.
 
Creation of Delete razor page
 
Now we will work on the delete function to delete a particular record from the database.
 
So upon clicking the delete button, an alert should be shown at the top and when the user clicks on ok, the record should be deleted from the index page. For that, we have to implement a popup inside the Ind.cshtml file.
 
Inside the delete button we need to add an onclick method which will return a message with a popup and then we want to go to an asp-page-handler (tag helper) on the same index page.
  1. @page  
  2. @model BookList.IndModel  
  3. <br />  
  4. <div class="container row p-0 m-0">  
  5.     <div class="col-10">  
  6.         <h2 class="text-info">Book List</h2>  
  7.     </div>  
  8.     <div class="col-2">  
  9.         <a asp-page="Create" class="btn btn-info form-control text-white">Create New Book</a>  
  10.     </div>  
  11.     <div class="col-12 border p-3 mt-3">  
  12.         <form method="post">  
  13.             @if(Model.Books.Count()>0)  
  14.             {  
  15.                 <table class="table table-striped border">  
  16.                     <tr class="table-secondary">  
  17.                         <th>  
  18.                             <label asp-for="Books.FirstOrDefault().Name"></label>  
  19.                         </th>  
  20.                         <th>  
  21.                             <label asp-for="Books.FirstOrDefault().Author"></label>  
  22.                         </th>  
  23.                         <th>  
  24.                             <label asp-for="Books.FirstOrDefault().ISBN"></label>  
  25.                         </th>  
  26.                         <th>  
  27.   
  28.                         </th>  
  29.                     </tr>  
  30.                     @foreach(var item in Model.Books)  
  31.                     {  
  32.                 <tr>  
  33.                     <td>  
  34.                         @Html.DisplayFor(m => item.Name)  
  35.                     </td>  
  36.                     <td>  
  37.                         @Html.DisplayFor(m => item.Author)  
  38.                     </td>  
  39.                     <td>  
  40.                         @Html.DisplayFor(m => item.ISBN)  
  41.                     </td>  
  42.                     <td>  
  43.                         <button asp-page-handler="Delete" asp-route-id="@item.Id" onclick="return confirm('Are you sure you want to delete?')" class="btn btn-danger btn-sm">Delete</button>  
  44.                         <a asp-page="Edit" asp-route-id="@item.Id" class="btn btn-success btn-sm text-white">Edit</a>  
  45.                     </td>  
  46.                 </tr>  
  47.                     }  
  48.                 </table>   
  49.             }  
  50.             else  
  51.             {  
  52.                 <p>No Books Available.</p>  
  53.             }  
  54.         </form>  
  55.     </div>  
  56. </div>  
We also need to pass the id of the book with the help of asp-route-id tag helper to delete a book of that particular id.
 
Performing Update And Delete Operations In ASP.NET Core 3 Razor Pages Using Microsoft SQL Server 
 
Creation of delete handler
 
Now we will implement a delete handler inside the index page.
  1. public async Task<IActionResult> OnPostDelete(int id)  
  2. {  
  3.     var book = await _db.Book.FindAsync(id);  
  4.     if (book == null)  
  5.     {  
  6.         return NotFound();  
  7.   
  8.     }  
  9.         _db.Book.Remove(book);  
  10.         await _db.SaveChangesAsync();  
  11.   
  12.         return RedirectToPage("Ind");  
  13. }  
Here we are using IActionResult as a return type because we will be redirecting to the same page. With the help of ID, we are checking whether book is available or not. If not, return not found message otherwise the book will be removed.
 
So this is how the delete functionality of the crud operations is implemented inside ASP.Net Core 3 Razor Pages.
 

Summary

 
In this article, I have implemented the Edit and Delete functionality of the crud operations inside the ASP.Net Core 3 razor page. I have implemented both edit post handler and delete post handler to perform their respective tasks. So with this, I have demonstrated how to perform crud operations in ASP.Net Core 3 razor page using the SQL Server database.