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 JoshiPosted Feb 11, 2023, 8:16 AM
Hello
Place your code for deleting category code at the parent level.
Example:
Thanks
Emmmanuel FIADUFEPosted Dec 20, 2022, 10:44 AM
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 FIADUFEPosted Dec 20, 2022, 9:12 AM
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)
Sachin SinghPosted Dec 20, 2022, 5:56 AM
yes, error is correct.
Place your delete inside ready function.
Or, you can also place the function at the bottom of your page both will work.
Emmmanuel FIADUFEPosted Dec 19, 2022, 11:31 PM
The error message says Delete not define