I want to delete record from table then update the table in MVC. Currently, I am able to delete the record from table but the table is not updating.I have written Script to update the partial but my script is not hitting.
Please suggest me how to do this?
Parent View
<div class="col-md-2">
@using (Html.BeginForm("HorseTracker", "HorseTracker", FormMethod.Post, new { @id = "CreateForm" }))
{
<div class="panel panel-default">
<div class="panel-body">
<div class="form-group">
@Html.LabelFor(m => m.HorseName, new { @class = "label-control" })
@Html.HiddenFor(m => m.HorseId)
@Html.TextBoxFor(m => m.HorseName)
</div>
<input type="submit" value="Save" class="btn btn-primary" />
</div>
</div>
}
</div>
<div id="partial" class="col-md-8">
Partial View
<div class="panel panel-default">
<div class="panel-body">
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.HorseName)
</th>
<th>
@Html.DisplayName("Action")
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td class="hidden">
@item.HorseId
</td>
<td>
@item.HorseName
</td>
<td>
@Ajax.ActionLink("Delete", "Delete", "HorseTracker", new {TrackerId = @item.TrackerId, AccId=item.AccId,UserId=item.UserId, @id = "DeleteRecord" }, new AjaxOptions
{
HttpMethod = "Post",
Confirm = "Are you sure you want to delete this record?"
}, null)
</td>
</tr>
}
</table>
</div>
</div>
Controller Method
[HttpPost]
public PartialViewResult Delete(int TrackerId, int AccId, int UserId)
{
ClsHorseTracker model = new ClsHorseTracker();
var username = User.Identity.Name;
var user = context.Users.SingleOrDefault(u => u.UserName == username);
model.AccId = user.AccountID;
model.UserId = user.Id;
horseTrackerDetails.HorseTrackerDelete(TrackerId);
return PartialView("_pHorseTrackerDetails", horseTrackerDetails.HorseTrackerList(model));
}
Script
$(document).ready(function () {
var url = '@Url.Action("Delete", "HorseTracker")';
$(document).click('#DeleteRecord', function () {
debugger;
if (!$(this).valid()) {
return;
}
$.post(url, $(this).serialize(), function (response) {
if (response) {
debugger;
$('#partial').html(response);
$('#CreateForm').get(0).reset();
}
});
return false;
})
})