我正在做一个MVC
项目,我试图在JQuery
使用@Url.Action
.
HTML 代码:
<button class="btn btn-white btn-sm demo1" data-id='@item.TeamID'>Delete</button>
jQuery代码:
$(document).ready(function () {
$('.demo1').click(function (event) {
swal({
title: "Are you sure?",
text: "You will not be able to recover this team!",
type: "warning",
showCancelButton: true,
confirmButtonColor: "#DD6B55",
confirmButtonText: "Yes, delete it!",
closeOnConfirm: false
}, function () {
var data = event.data;
var id = data.id;
var url = '@Url.Action("Delete", "Teams", new { id = "__param__" })';
window.location.href = url.replace('__param__', encodeURIComponent(id));
swal("Deleted!", "Your team has been deleted.", "success");
});
});
});
但是,不会触发 Teams 控制器中的 Delete 方法。我错过了什么吗?
更新:
HTML 按钮放置在foreach
循环内:
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.TeamName)
</td>
<td>
@Html.DisplayFor(modelItem => item.TeamInits)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.TeamID }, new { @class = "btn btn-white btn-sm" })
<button class="btn btn-white btn-sm demo1" data-id='@item.TeamID'>Delete</button>
</td>
</tr>
}