HTML PAGE
function Delete(CategoryId) {
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function(isConfirm) {
if (isConfirm) {
swal("Deleted!", "Your record has been deleted.", "success");
$.get("/Home/Delete", { CategoryId: CategoryId }, function (res) {
if (res) {
dataTable.ajax.reload();
}
})
} else {
swal("Cancelled", "Your imaginary file is safe :)", "error");
}
});
Home Controller
public ActionResult Delete(int CategoryId)
{
try
{
ASPNETMASTERPOSTEntities db = new ASPNETMASTERPOSTEntities();
var categories = db.tblCategories.Find(CategoryId);
db.tblCategories.Remove(categories);
db.SaveChanges();
return Json(true, JsonRequestBehavior.AllowGet);
}
catch (Exception)
{
return Json(false, JsonRequestBehavior.AllowGet);
}
}

Vishal JoshiRank #377 · 6.7K reputationPosted Feb 11, 2023, 3:16 AM EST
Hello
Place your code for deleting category code at the parent level.
Example:
$(document).ready(function() { function Delete(CategoryId) { swal({ title: "Are you sure?", text: "You will not be able to recover this imaginary file!", type: "warning", showCancelButton: true, confirmButtonClass: "btn-danger", confirmButtonText: "Yes, delete it!", cancelButtonText: "No, cancel plx!", closeOnConfirm: false, closeOnCancel: false }, function(isConfirm) { if (isConfirm) { $.get("/Home/Delete", { CategoryId: CategoryId }, function (res) { if (res) { dataTable.ajax.reload(); swal("Deleted!", "Your record has been deleted.", "success"); } }) } else { swal("Cancelled", "Your imaginary file is safe :)", "error"); } }); })Thanks
Sachin SinghRank #66 · 26.2K reputationPosted Dec 20, 2022, 12:56 AM EST
yes, error is correct.
Place your delete inside ready function.
$(function(){ function Delete(CategoryId) { swal({ title: "Are you sure?", text: "You will not be able to recover this imaginary file!", type: "warning", showCancelButton: true, confirmButtonClass: "btn-danger", confirmButtonText: "Yes, delete it!", cancelButtonText: "No, cancel plx!", closeOnConfirm: false, closeOnCancel: false }, function(isConfirm) { if (isConfirm) { swal("Deleted!", "Your record has been deleted.", "success"); $.get("/Home/Delete", { CategoryId: CategoryId }, function (res) { if (res) { dataTable.ajax.reload(); } }) } else { swal("Cancelled", "Your imaginary file is safe :)", "error"); } }); })Or, you can also place the function at the bottom of your page both will work.
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 20, 2022, 5:44 AM EST
I re write it serverally but not working, the same error messsage. "Uncaught ReferenceError: Delete is not defined
at HTMLAnchorElement.onclick (Category:1:1)"
$(function () {
function Delete(CategoryId) {
swal({
title: "Are you sure?",
text: "You will not be able to recover this imaginary file!",
type: "warning",
showCancelButton: true,
confirmButtonClass: "btn-danger",
confirmButtonText: "Yes, delete it!",
cancelButtonText: "No, cancel plx!",
closeOnConfirm: false,
closeOnCancel: false
},
function (isConfirm) {
if (isConfirm) {
$.get("/Home/Delete", { CategoryId: CategoryId }, function (res) {
if (res) {
dataTable.ajax.reload();
}
})
swal("Deleted!", "Your record has been deleted.", "success");
} else {
swal("Cancelled", "Your record is safe :)", "error");
}
});
}
});
}
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 19, 2022, 6:31 PM EST
The error message says Delete not define
Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Dec 20, 2022, 4:12 AM EST
Hello Schin, I used your code but this is the error message am getting.
Uncaught ReferenceError: Delete is not defined
at HTMLAnchorElement.onclick (Category:1:1)