[英]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.