skip to Main Content

When pressing the delete button it successfully delete the record from database. But not delete from the frontend it gives result after reloading or refreshing the page.

My view:

@foreach (var item in Model)
{
<a href="#" class="phone_number" onclick="del(this)" data-id="@item.id">
 <i class="fas fa-trash-alt"></i>
</a> 
}
<script>
 function del(x) {
            var url = '@Url.Action("deleteRent", "Home")';
            
            var rd = x.dataset.id
            debugger

            $.ajax({
                url: url,
                type: 'POST',
                data: {
                    id: rd
                },
                success: function (data) {
                    if (data.length == 0) // No errors
                        alert("Delete success!");
                },
                error: function (jqXHR) { // Http Status is not 200
                },
                complete: function (jqXHR, status) { // Whether success or error it enters here
                }
            });
        };
       </script>

2

Answers


  1. Please add window.location.reload() code after alert….

    success: function (data) {
                    if (data.length == 0) // No errors
                        alert("Delete success!");
                        **window.location.reload();**
                },
    

    this code automatically reload your page after success

    Login or Signup to reply.
  2. Actually you delete from database but you don’t refresh your page.

    Either, you use answer from Dipendrasinh Vaghela and you refresh the whole page.

    Or if you have a function which search and show in DOM, you call it when delete is success. That’ll update "only" the part showing datas.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search