Friends What happens is that I need to capture the value of an attribute of an html tag in this case.
<input class="btn btn-danger link1" type="button" value="Delete" data="@item.MunicipalityId" />
I have this tag inside a cycle for that is, it creates approximately 3 elements with the same class 'link1'
<table class="table">
@foreach (var item in Model.Municipalities)
{
<tr>
<td>
@Html.DisplayFor(x => item.Name)
</td>
<td>
@Html.ActionLink("Edit", "EditMunicipality", new { id = item.MunicipalityId }, new { @class = "btn btn-warning" }) |
<input class="btn btn-danger link1" type="button" value="Delete" data="@item.MunicipalityId" />
</td>
</tr>
}
</table>
and I need to make JQuery capture that value to the button that the user clicks on, this means that each one button of the cycle will bring me a different data attribute. I've done it like that
<script>
$('.link1').click(function () {
swal({
title: 'Are you sure?',
text: 'You will not be able to recover this imaginary file!',
type: 'warning',
showCancelButton: true,
confirmButtonColor: '#DD6B55',
confirmButtonText: 'Yes, delete it!',
closeOnConfirm: false
}, function () {
$.ajax({
type: "POST",
url: '@Url.Action("DeleteMunicipality")',
data: { MunicipalityId: $(this).attr("data") },
datatype: "html",
//success: function (data) {
// //codigo
//}
success: function () {
var MunicipalityId = $(this).attr("data");
if (MunicipalityId == null) {
swal('Deleted!',
'Your imaginary file has been deleted.',
'success'
);
} else {
swal("Oops", "The record can't be delete.(has related records)", "error");
}
},
error: function (ex) {
swal("Oops", "We couldn't connect to the server!" + ex, "error");
}
});
return false;
});
});
</script>
And I always capture is the first of the list, regardless of the button that you click. Thanks is what you can help me.