CRUD Operations In ASP.NET Core 1.0 MVC Application - Part Six

Let’s implement Delete Contact for our Contacts Application. We are going to extend our application from our last discussio about CRUD.
Add DeleteContact Method in ContactBusinessLogic
  • Open existing Solution in Visual Studio 2015.
  • Open WebApplicationCore.NetCore.BusinessLogic.ContactBusinessLogic class.
  • Add new DeleteContact method.
  • It may delete existing contact from mockup list data.
  1. public bool DeleteContact(int contactId) {  
  2.     bool deleted = false;  
  3.     Contact contact = ContactBusinessLogic.Contacts.FirstOrDefault(c => c.ContactId == contactId);  
  4.     if (contact != null) {  
  5.         ContactBusinessLogic.Contacts.Remove(contact);  
  6.         deleted = true;  
  7.     }  
  8.     return deleted;  
  9. }  
 ContactController
 
Add DeleteContact Action Methods in ContactController
  • Add two new DeleteContact Methods.

    • One DeleteContact method with ContactId parameter and HttpGet attribute to explicitly specify that this is get method. It will take user to confirmation page.

    • Another DeleteContact method with parameter of ContactVM type and HttpPost attribute to explicitly specify that this is post method. It will be called if user confirms to delete contact, after deleting contact list page will be loaded.
  • It looks odd to have two methods to delete a simple item. We can perform this task with single delete method and even without involving a view. Alternatively, we could have some confirmation dialog to perform delete, or some other solution with single delete method. I also had such questions in my mind when I read about this practice for the first time. Let me summarize this for you,

    • It may create a security hole as anyone can use use a direct link to attack and to delete data. Even a search engine crawler can hit this method unnecessarily. Although we can use Authorize filter to control Delete method call, yet I am convinced that it is an improvisation, not a best solution.

    • And as per REST standards, Get method may not change data. And therefore it is not a good practice.

    • Simply put, it is not a good practice to perform a delete operation with a get call.
  1. [HttpGet]  
  2. public IActionResult DeleteContact(int id) {  
  3.         ContactVM contactVM = new ContactVM {  
  4.             ContactId = id  
  5.         };  
  6.         return View(contactVM);  
  7.     }  
  8.     [HttpPost]  
  9. public IActionResult DeleteContact(ContactVM contactVM) {  
  10.     ContactBusinessLogic contactBL = new ContactBusinessLogic();  
  11.     contactBL.DeleteContact(contactVM.ContactId);  
  12.     return RedirectToAction("Index");  
  13. }  
 
 
Add Contact DeleteContact View
  • Add new View to Contact\Contact folder.
  • Open Add New Item Screen through Solution Context Menu of Contact >> Add >> New Item >> Installed >> .NET Core >> MVC View Page.
  • Name it DeleteContact.cshtml.
  • Click OK Button.
  • It will add a new view in Contact view folder.

    • Now, we have ContactVM objects as model.
    • Delete button to delete contact and Cancel button to return back to list page without deleting contact.
    • Change Index View implementation to add Delete Details option with each record.
  1. @model ContactVM  
  2.     <  
  3.     h2 > Delete Contact < /h2> <  
  4.     form asp - action = "DeleteContact" >  
  5.     <  
  6.     input type = "hidden"  
  7. asp -  
  8.     for = "ContactId" / >  
  9.     <  
  10.     div class = "form-horizontal" >  
  11.     <  
  12.     div class = "form-group" >  
  13.     <  
  14.     label > Please confirm to delete contact < /label> <  
  15.     div class = "col-md-10" >  
  16.     <  
  17.     input type = "submit"  
  18. value = "Delete"  
  19. class = "btn btn-default" / >  
  20.     <  
  21.     a asp - action = "Index"  
  22. class = "btn btn-default" > Cancel < /a> <  
  23.     /div> <  
  24.     /div> <  
  25.     /div> <  
  26.     /form>  
  1. <td>  
  2.     <a asp-controller="Contact" asp-action="GetContact" asp-route-id="@item.ContactId">Get Details</a> |  
  3.     <a asp-controller="Contact" asp-action="UpdateContact" asp-route-id="@item.ContactId">Edit Details</a> |  
  4.     <a asp-controller="Contact" asp-action="DeleteContact" asp-route-id="@item.ContactId">Delete Details</a>  
  5. </td>  
 
 
Run Application in Debug Mode
  • Press F5 or Debug Menu >> Start Debugging or Start IIS Express Button on Toolbar to start application in debugging mode.
  • It will show Home Page in browser.
  • Click Contact List Menu Open to open Contact List Page.
  • Click Delete Details link to open Delete Contact Page and URL will change to http://localhost:21840/Contact/DeleteContact/3.
  • Click Delete button. It will delete contact and it will load contact list page.

     
Sample Source Code

I have placed the sample code for this session in "CRUD operations in ASP.NET Core 1.0 MVC Application Part 6_Code.zip" in CodePlex repository.

Up Next
    Ebook Download
    View all
    Learn
    View all