Friday, August 9, 2019

Part 3 - Basic CRUD operations using Jquery ajax and modal popup in ASP.NET MVC4.

In this post, I am going to implement Basic CRUD (Create, Read, Update, Delete) operations using Jquery ajax and modal popup in ASP.NET MVC4 application.
I have split the entire application split into following 3 parts for making things more simple and understandable specially for beginners.

  • Part 1 : Perform read functionality of CRUD Operation.
  • Part 2 : Perform create and update functionality of CRUD Operation.
  • Part 3 : Perform delete functionality of CRUD Operation.

Step-1: Add new action into your controller for get view for Delete contact. Here I have used "Delete" Action. Please write below code
  1. public ActionResult Delete(int id)
  2. {
  3. var c = GetContact(id);
  4. if (c == null)
  5. {
  6. return HttpNotFound();
  7. }
  8. return PartialView(c);
  9. }

Step-2: Add partial view for the "Delete" Action & design.

Right Click on Shared folder (under Views) > Add > View... > Enter View Name > Select View Engine (Razor) > Check "Create a strong-typed view" > Select your model class > Check "Create as a Partial View" > Add. 
[N:B:Please Rebuild solution before add view.] 

Step-3: Add an another action for POST method for Delete contact from the database.

Please write below code
  1. [HttpPost]
  2. [ValidateAntiForgeryToken]
  3. [ActionName("Delete")]
  4. public ActionResult DeleteContact(int id)
  5. {
  6. bool status = false;
  7. string message = "";
  8. using (MyDatabaseEntities dc = new MyDatabaseEntities())
  9. {
  10. var v = dc.Contacts.Where(a => a.ContactID.Equals(id)).FirstOrDefault();
  11. if (v != null)
  12. {
  13. dc.Contacts.Remove(v);
  14. dc.SaveChanges();
  15. status = true;
  16. message = "Successfully Deleted!";
  17. }
  18. else
  19. {
  20. return HttpNotFound();
  21. }
  22. }
  23.  
  24. return new JsonResult { Data = new { status = status, message = message } };
  25. }

Step-4: Add an javascript function (DeleteContact) for Delete Contact

Open javascript file (we have created in part 1 step 7, here "dotnetawesome.CRUDContacts.js") and write following code 
  1. //Delete Contact
  2. function DeleteContact() {
  3. $.ajax({
  4. url: '/home/delete',
  5. type: 'POST',
  6. dataType: 'json',
  7. data: {
  8. 'id': $('#ContactID').val(),
  9. '__RequestVerificationToken': $('input[name=__RequestVerificationToken]').val()
  10. },
  11. success: function (data) {
  12. alert(data.message);
  13. if (data.status) {
  14. $dialog.dialog('close');
  15. LoadContacts();
  16. }
  17. },
  18. error: function () {
  19. $('#msg').html('<div class="failed">Error ! Please try again.</div>');
  20. }
  21. });
  22. }
Add following jquery code inside $(document).ready for delete contact from database
  1. //Delete Contact
  2. $('body').on('submit', '#deleteForm', function (e) {
  3. e.preventDefault();
  4. DeleteContact();
  5. });

Step-5: Run Application.

No comments:

Post a Comment

How to register multiple implementations of the same interface in Asp.Net Core?

 Problem: I have services that are derived from the same interface. public interface IService { } public class ServiceA : IService { ...