Home > Software design >  delete button delete record from the database but not from the frontend
delete button delete record from the database but not from the frontend

Time:11-16

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="#"  onclick="del(this)" data-id="@item.id">
 <i ></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>

CodePudding user response:

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.

CodePudding user response:

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

  • Related