Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 852
  • 829
  • 37.2k

Sweetalert is not working in my MVC ASP.NET Application

Feb 10 2023 1:56 PM

DataTable delete Button
{
             "data": "ProductId", "width": "50px", "render": function (data) { return "<a  style=color:red class='fa fa-trash-alt ' onclick=DeleteProduct('" + data + "');>Delete</a>"; }
         },

JAVASCRIPT
function DeleteProduct(ProductId) {
            swal({
                title: 'Do you want to delete Data?',
                showCancelButton: true,
                showConfirmButton: true,
                confirmButtonText: 'Yes Delete',
                cancelButtonText: 'No pls cancel',
                confirmButtonClass: 'btn btn-success',
                cancelButtonClass: 'btn btn-danger',
                type: 'warning',
                buttonsStyling: false
            }).then(function (isConfirm) {
                // Called if you click Yes.
               
                if (isConfirm) {
                    // Make Ajax call.
                    $.ajax({
                        type: "POST",
                        url: "/Home/DeleteProduct" + ProductId,
                        data: { ProductId: ProductId },
                        contentType: 'application/json; charset=ut-8',
                        dataType: 'json',
                    })
                    
            swal('Confirmed', '1 Product deleted successfully', 'success');
        }
        },
        function (no) {
            // Called if you click No.
            if (no == 'cancel') {
                swal('Cancelled', '', 'error');
            }
        
        });
       }
      


HomeController
 public ActionResult DeleteProduct(int ProductId)
        {
            try
            {
                ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
                var product = db.tblProducts.Find(ProductId);
                db.tblProducts.Remove(product);
                db.SaveChanges();
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            catch (Exception)
            {
                return Json(false, JsonRequestBehavior.AllowGet);
            }

        }


Answers (2)