When I press the delete button I get the alert with the delete message, and then when I press the delete button, the record is NOT deleted. The problem is that when you press the delete button, the AJAX method is not executed.
events
<script>
//Aquí hago que aparezca la alerta acompañado del valor del "id".
$(document).ready(function () {
$(".btn-eliminar").on("click", function () {
Swal.fire({
title: 'Está seguro que desea eliminar esta Charla?',
text: "No podrá recuperar los datos!",
type: 'warning',
showCloseButton: true,
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Sí, eliminarlo!',
cancelButtonText: 'Cancelar',
html: `<div class="lbl-id-oculto">${$(this).parent().siblings(".td-id").text().trim()}</div>`
})
});
//Aquí esta el código de la acción eliminar, cuando presiono eliminar le envió el parametro "id" para que sepa que registro eliminar, pero NO ELIMINA NADA.
$(document).on("click", ".swal2-confirm", function () {
$.ajax({
type: "POST",
url: '@Url.Action("EliminarCharla", "Charlas")',
data: { id: $(this).parent().siblings(".swal2-content").find(".lbl-id-oculto").text().trim() },
success: function (rpta) {
},
error: function (req, textStatus, errorThrown) {
alert('Ooops, something happened: ' + textStatus + ' ' + errorThrown);
}
});
});
});
</script>
Controller
public ActionResult EliminarCharla(string id)
{
ClsConexion con = new ClsConexion();
var Cnx = con.Conexion();
OracleCommand cmd = new OracleCommand("SIMEXA_SP_ELIMINAR_CHARLA", Cnx);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("param_id", OracleDbType.Varchar2)).Value = id;
Cnx.Open();
OracleTransaction tx = Cnx.BeginTransaction();
cmd.ExecuteNonQuery();
tx.Commit();
Cnx.Close();
cmd.Dispose();
Cnx.Dispose();
return RedirectToAction("MostraCharlas");
}
As such , it
SweetAlert
has its way of validating the selected option, this is done through athen
.I hope it helps.