I have several inputs that I charge with a foreach with Razor.
<table class="table">
@foreach (var item in Model.Municipalities)
{
<tr>
<td>
@Html.DisplayFor(x => item.Name)
</td>
<td>
<input class="btn btn-danger" type="button" value="Delete" onclick="SweetAlert(@item.MunicipalityId)" />
</td>
</tr>
}
</table>
As you can see, it has an onclick event and I need that when it does succeed, it is deleted without loading the page and this is what I have but it does not work for me.
function () {
$.ajax({
type: "POST",
url: '@Url.Action("DeleteMunicipality")',
data: { MunicipalityId: id },
datatype: "html",
//success: function (data) {
// //codigo
//}
success: function () {
swal('Deleted!',
'Your imaginary file has been deleted.',
'success'
);
$(this).remove();
},
error: function () {
swal("Oops", "We couldn't connect to the server! or The record can't be delete.(has related records)", "error");
}
});
return false;
}