我们希望在删除前显示警告用户的提示。目前我们正在尝试使用模态但我们无法访问项目ID,我们不知道如何将其传递给cshtml中的模态。
这就是我们如何显示我们想让用户删除的项目:
@foreach (var role in Model.AllRoles)
{
<tr>
<td>@role.Name</td>
<td>
<a asp-area="SysAdmin" asp-controller="Role" asp-action="EditRole" asp-route-id="@role.Id" class="btn btn-sm btn-success" style="width: 100%">Edit</a>
</td>
<td>
<button type="submit" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#deleteRoleModal" style="width: 100%">
Delete
</button>
</td>
</tr>
}
因此删除按钮会显示一个模态,目前看起来像这样:
<div class="modal fade" id="deleteRoleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Role</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Are you sure you want to delete this role?</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<a class="btn btn-primary" asp-area="SysAdmin" asp-controller="Role" asp-action="DeleteRole">Delete Role</a>
</div>
</div>
</div>
</div>
因此,当用户单击删除角色时,他们将在Controller角色上调用Action DeleteRole,后者将获取ID。但是,由于模式不知道要删除的对象的ID,我们无法将其传递给Action。
所以问题是我们如何将表中的ID发送到模态,以便在用户想要删除时将其传递给Action?
答案 0 :(得分:0)
试试这个,
@foreach (var role in Model.AllRoles)
{
<tr>
<td>@role.Name </td>
<td>
<a asp-area="SysAdmin" asp-controller="Role" asp-action="EditRole" asp-route-id="@role.Id" class="btn btn-sm btn-success" style="width: 100%">Edit</a>
</td>
<td>
<button onclick="SetId(@role.Id)" type="submit" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#deleteRoleModal" style="width: 100%">
Delete
</button>
</td>
</tr>
}
模态
<div class="modal fade" id="deleteRoleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Role</h5>
<button class="close" type="button" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">Are you sure you want to delete this role?</div>
<div class="modal-footer">
<button class="btn btn-secondary" type="button" data-dismiss="modal">Cancel</button>
<button onclick="DeleteRole" class="btn btn-primary">Delete Role</button>
</div>
</div>
</div>
</div>
脚本标记
<script>
var selectedId = 0;
var deleteURL = '@Url.Action("DeleteRole", "Role", new { Area = "SysAdmin" })';
SetId(id)
{
selectedId = id;
}
DeleteRole()
{
if (selectedId !== 0)
{
deleteURL = deleteURL + "?id=" + selectedId;
window.open(deleteURL,"_self")
}
}
</script>