制作一个确认删除的功能:
    image.png

    视图的修改:

    1. <div class="card-footer">
    2. <form method="post" asp-action="DeleteUser" asp-route-id="@user.Id">
    3. @*这个 a 标签通过 GET 请求,不会和下面的 submit 冲突*@
    4. <a asp-action="EditUser" asp-controller="Admin" asp-route-id="@user.Id" class="btn btn-primary">编辑</a>
    5. <span id="confirmDeleteSpan_@user.Id" style="display: none">
    6. <span>确定删除?</span>
    7. <button type="submit" class="btn btn-danger"></button>
    8. <a href="#" class="btn btn-primary" onclick="confirmDelete('@user.Id', false)"></a>
    9. </span>
    10. <span id="deleteSpan_@user.Id">
    11. <a href="#" class="btn btn-danger" onclick="confirmDelete('@user.Id', true)">删除</a>
    12. </span>
    13. </form>
    14. </div>
    15. ...
    16. @section Scripts{
    17. <script src="~/js/CustomScript.js" asp-append-version="true"></script>
    18. }

    CustomScript 控制视图显式效果:

    1. function confirmDelete(uniqueId, isDeleteClicked) {
    2. var deleteSpan = "deleteSpan_" + uniqueId;
    3. var confirmDeleteSpan = "confirmDeleteSpan_" + uniqueId;
    4. if (isDeleteClicked) {
    5. $("#" + deleteSpan).hide();
    6. $("#" + confirmDeleteSpan).show();
    7. } else {
    8. $("#" + deleteSpan).show();
    9. $("#" + confirmDeleteSpan).hide();
    10. }
    11. }
    1. a 标签对应 GET 请求,form + submit 对应 POST 请求,互不冲突
    2. 通过 jQuery 实现简单的动态效果