I'm using Ajax to delete a row from my Roles table. I can add the role and allow the user to delete the role from a link for each row. The function works and deletes row in database but the table is not updated after the event. It looks like the row never got deleted to the user. Do I need to reload the view? Not sure how to make the screen refresh to reflect only the remaining rows.
<div id="roleTable">
<table class="table">
<tr><th>Role</th><th>Description</th><th>Delete</th></tr>
@{int count = 0;}
@foreach (var b in ViewBag.roles)
{
string color = "";
if (count % 2 == 0)
{
color = "";
}
else
{
color = "success";
}
count++;
<tr class=@color id="roleRow-@b.RoleGUID">
<td>@b.Role</td>
<td>@b.Description</td>
<td><a href="#" class="removeLink" data-id="@b.RoleGUID" onclick="return confirm('Are you sure');">Remove</a></td>
</tr>
}
</table>
</div>
<p id="result">
</p>
<script>
$("#roleTable").on("click",".removeLink", function () {
var roleToDelete = $(this).attr("data-id");
if (roleToDelete != '') {
$.post("/UserRoles/RemoveRole", { "RoleGUID": roleToDelete },
function (data) {
$("#roleRow-" + roleToDelete).fadeOut('slow');
$("#result").text(data.Message).fadeOut(5000);
});
}
});
</script>