Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 778
  • 1.1k
  • 55.2k

ASP.NET Delete query is not working

Apr 27 2024 11:20 AM

Hello Team,

When I click the delete button and the sweetalert popup and the option to delete and cancel shows, and when I click delete the data doesn't get deleted and in the function when I remove == sign, either I chose cancel or delete, the data get deleted mean while when I click on cancel the data is not suppose to delete.

 function DeleteClass(ClassId) {
        swal({
            title: 'Do you want to delete this Data?',
            showCancelButton: true,
            showConfirmButton: true,
            confirmButtonText: 'Yes Delete',
            cancelButtonText: 'No pls cancel',
            confirmButtonClass: 'btn btn-success',
            cancelButtonClass: 'btn btn-danger',
            type: 'warning',
            closeOnConfirm: false,
            closeOnCancel: false
        }).then(function (Confirm) {
            // Called if you click Yes.

            if (Confirm == true) {
                // Make Ajax call.
                $.get("/Home/DeleteClass", { ClassId: ClassId }, function (res) {
                    if (res) {
                        dataTable.ajax.reload();
                    }

                })

                swal('Confirm', 'Class successfully deleted!', 'success');
            }
        },
    function (no) {
        // Called if you click No.
        if (no == 'cancel') {
            swal('Cancelled', '', 'error');
        }

    });

    }
 

  public ActionResult DeleteClass(int? ClassId)
        {
            try
            {
                tblClass deleteClass = objBasicShoolDBEntities.tblClasses.Where(a => a.ClassId == ClassId).FirstOrDefault<tblClass>();
                objBasicShoolDBEntities.tblClasses.Remove(deleteClass);
                objBasicShoolDBEntities.SaveChanges();
                return Json(true, JsonRequestBehavior.AllowGet);

            }
            catch (Exception)
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }
        }


Answers (3)