I'm not sure how to refresh data after I use AJAX. Here's what I already have:
Frontend:
@model TFU.Model.DB_USER <div id="listTelNumbers"> @foreach (var item in Model.DB_USER_PHONES) { <dl class="dl-horizontal"> <dt> @item.PHONE </dt> <dd> <button id="removeButton" class="btn btn-default" onclick="sendRequestToRemove('@item.USER_ID', '@item.PHONE')">Usuń</button> </dd> </dl> } </div> Script - fadeOut works fine but I don't know what should I fadeIn. So I guess is missing a small part of code there. Also how can I fadeOut only the record which I currently removing instead all list.
<script> function sendRequestToRemove(id, phone) { var data = { "USER_ID": id, "PHONE": phone } $.ajax({ url: '/User/RemoveTelNumber', data: JSON.stringify(data), type: 'POST', contentType: 'application/json; charset=utf-8', error: function (err) { alert('Error: ' + err.statusText); }, success: function (result) { $('#listTelNumbers').fadeOut(800, function () { form.html('#listTelNumbers').fadeIn().delay(2000); }); }, async: true, processData: false }); } </script> Backend:
public bool RemoveTelNumber(DB_USER_PHONES model) { var user = db.DB_USER_PHONES.First(x => x.USER_ID == model.USER_ID && x.PHONE == model.PHONE); db.DB_USER_PHONES.Remove(user); db.SaveChanges(); return true; }
<dl>element containing the button you clicked from the<div id="listTelNumbers">element after the ajax call?<dt>. In this example (prntscr.com/bf25u1) after i'll clickRemovethen in div listTelNumbers will left 3 elements.