Hi Guys,
Delete Confirmation using SweetAlert in .NET CORE 5.0 MVC
I'm trying to make delete confirmation for user. When user click this button in Index.cshtml it will show delete confirmation for delete data or not, but delete confirmation just appear for while after that data deleted without the confirmation.
Please correct my code sir. Any help could be appreciate
<td style="width:15%">
<a href="@Url.Action("Delete","Petugas", new {@Kode_Petugas=@Model.Rows[i]["Kode_Petugas"]})" onclick="AlertDeleteData();"><i class="fa fa-trash btn-sm btn-danger"> Delete</i></a>
</td>
This is the Controller for delete
// GET: PetugasController/Delete/5
public ActionResult Delete(string Kode_Petugas)
{
using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("db_perpustakaan")))
{
using (SqlCommand cmd = new SqlCommand("spEditPetugas"))
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Delete From Petugas Where Kode_Petugas = @Kode_Petugas";
cmd.Parameters.AddWithValue("@Kode_Petugas", Kode_Petugas);
cmd.ExecuteNonQuery();
}
return RedirectToAction("Index");
}
}
This is the SweetAlert Delete Confirmation
<script type="text/javascript" lang="javascript">
function AlertDeleteData() {
Swal.fire({
title: 'Apakah yakin akan menghapus data ini ???',
text: "Jika sudah dihapus data tidak dapat dikembalikan lagi.",
icon: 'error',
buttons: true,
showCancelButton: true,
dangerMode: true
}).then((result) => {
if (result.isConfirmed) {
Swal.fire({
text: 'Data berhasil dihapus...',
icon: 'success'
});
}
else {
Swal.fire({
text: 'Data batal dihapus...',
icon: 'info'
});
}
return false;
});
}
</script>