繁体   English   中英

从数据库中删除记录后重新加载页面(表)的问题

[英]Problem in reloading page(table) after deleting record from database

我有一个ASP.NET CORE MVC应用程序,其中列出了一些客户端(CRUD),我可以在单击删除时删除用户。

我试图解决的问题是当我从列表中删除记录后刷新页面时,列表仍然显示已删除的记录,当在数据库中删除记录时,如果我手动重新加载(F5)页面记录消失。

我已经尝试了location.reload(),windows.location.reload(),没有...我可以看到该页面正在重新加载,但记录不会消失。

我的代码如上:

<script type="text/javascript">

        function toggleChecked(status) {
            $("#checkboxes input").each(function () {

                // Set the checked status of each to match the
                // checked status of the check all checkbox:
                $(this).prop("checked", status);
            });
        }

        function Delete(id) {
            var example_table = $("#example1")
            var r = confirm("Are you sure you want to Delete?");
            if (r == true) {

                $.ajax(
                {
                    type: "POST",
                    url: '@Url.Action("Delete", "Clients")',
                    data: {
                        id: id
                    },
                    error: function (result) {
                        alert("error");
                    },
                    success: function (result) {
                        if (result == true) {
                            example_table.ajax.reload(); // -->> The problem is in this line!
                            location.reload(); // -->> The problem is in this line!
                        }
                        else {
                            alert("There is a problem, Try Later!");
                        }
                    }
                });
            }

        }



        $(document).ready(function () {
            //Set the default value of the global checkbox to true:
            $("#checkall").prop('checked', false);

            // Attach the call to toggleChecked to the
            // click event of the global checkbox:
            $("#checkall").click(function () {
                var status = $("#checkall").prop('checked');
                toggleChecked(status);
            });
        });


    </script>

后端删除:

[HttpPost]
        public bool Delete(int id)
        {
            try
            {
                Clients client = db.Clients.Where(s => s.Id == id).First();
                db.Clients.Remove(client );
                db.SaveChanges();
                return true;
            }
            catch (System.Exception)
            {
                return false;
            }

        }

我希望删除的记录实时消失,而不必手动刷新页面。 如果你能帮助我,我很感激。

如果删除成功,您可以在ajax调用成功时从列表中手动删除该项。 但是,重新加载时,页面的重新加载是否会调用数据库并获取数据列表? 如果没有从重新加载触发,那么它将不会更新列表。 如果是那么我唯一的建议是cookie可能存储列表?

对于服务器端刷新,您可以使用Response.Redirect(Request.RawUrl)

对于客户端刷新,您可以使用window.location.href= window.location

或者document.location.reload()而不是location.reload()

暂无
暂无

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

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