简体   繁体   English

在 ASP.NET 中删除带有 ajax 的行后不显示更新的 DataTable MVC 需要重新加载页面

[英]Not showing the updated DataTable after removing the row with ajax in ASP.NET MVC need to reload the page

I have a DataTable to display the book data.我有一个数据表来显示图书数据。 After clicking the delete button, the book record is successfully deleted from the database.点击删除按钮后,图书记录成功从数据库中删除。 However, I have to reload the page for the updated the DataTable.但是,我必须重新加载更新后的 DataTable 的页面。

How can I delete the row in UI?如何删除 UI 中的行?

View:看法:

<table class="table table-bordered table-hover" id="books">
    <thead>
        ...
    </thead>
    <tbody>
        @foreach (var book in model)
        {
            <tr>
                <td>@Html.ActionLink(book.Name, "Details", "Books", new { id = book.Id })</td>
                <td>@book.Author</td>
                <td>@book.Type.Type</td>
                <td>@book.NumberInStock</td>
                <td> 
                    //Delete Button
                    <button data-book-id="@book.Id" class="btn btn-danger js-delete book-delete">Delete</button> 
                </td>
            </tr>
        }
    </tbody>
</table>

Ajax call: I don't want to use location.reload(); Ajax 电话:我不想使用location.reload(); to reload the page重新加载页面

$(document).ready(function () {
        var myDataTable = $("#books").DataTable({
            "drawCallback": function (settings) {
                $("#books .book-delete").on("click", function (e) {
                    var button = $(this);
                    var result = confirm('Are you sure you wish to delete this book?');
                    if (result) {
                        var form = $('#__AjaxAntiForgeryForm');
                        var token = $('input[name="__RequestVerificationToken"]', form).val();
                        $.ajax({
                            url: "/Books/Delete/" + button.attr("data-book-id"),
                            method: "POST",
                            data: {
                                __RequestVerificationToken: token,
                                someValue: "Your Custom Secret String"
                            },
                            success: function () {
                                location.reload();
                            },
                            statusCode: {
                                500: function () {
                                }
                            }
                        });
                    }
                });
            }
        });
    });

Can you try this?你能试试这个吗? I can't try it my self.我不能自己尝试。

   $(document).ready(function () {
      var myDataTable = $("#books").DataTable({
        "drawCallback": function (settings) {
            $("#books .book-delete").on("click", function (e) {
                var button = $(this);
                var result = confirm('Are you sure you wish to delete this book?');
                if (result) {
                    var form = $('#__AjaxAntiForgeryForm');
                    var token = $('input[name="__RequestVerificationToken"]', form).val();
                    $.ajax({
                        url: "/Books/Delete/" + button.attr("data-book-id"),
                        method: "POST",
                        data: {
                            __RequestVerificationToken: token,
                            someValue: "Your Custom Secret String"
                        }
                    myDataTable.row($(this).parents('tr')).remove().draw();
                   // you can also try this 
                   // $('#books').DataTable().ajax.reload();
                    });
                }
            });
        }
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM