Pagination using AJAX(I made a PartialView so page is not going to refresh every time when yser click Next button to view next record)
While retrieving Data from 1 table it works perfectly fine,but it throws error while fetching data from 2 tables using navigational properties when click Next button :" An error occurred while executing the command definition "
Anyone has any idea where im doing wrong?
What I have tried:
Controller :
I wrote this code in Index() ------ This is HttpGet
var tsk = db.Tasks.Include(e => e.Customers);
ViewBag.CurrentPage = 1;
ViewBag.LastPage = Math.Ceiling(Convert.ToDouble(tsk.ToList().Count)/5);
return View(tsk.Take(5));
And for HttpPost Index ()
ViewBag.CurrentPage = CurrentPage;
ViewBag.LastPage = LastPage;
return PartialView("_Task", db.Tasks.OrderBy(v => v.TaskID).Skip((CurrentPage - 1) * 5).Take(5));
View :
<div style="width: 600px;">
@Html.ActionLink("Add Task", "Create", "Home", null)
<div id="TaskDiv">
@Html.Partial("_Task", Model)
</div>
</div>
<div>
@using (Ajax.BeginForm(new AjaxOptions { HttpMethod = "POST", InsertionMode = InsertionMode.Replace, UpdateTargetId = "TaskDiv" }))
{
<input type="hidden" id="CurrentPage" name="CurrentPage" value="@ViewBag.CurrentPage" />
<input type="hidden" id="LastPage" name="LastPage" value="@ViewBag.LastPage" />
}
<input type="button" id="Previous" value="Previous" />
<input type="button" id="Next" value="Next" />
</div>
I also wrote some JQuery and JS code
Partial View
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@item.Customers.Name</td>
<td>@item.Name</td>
<td>@item.StartDate</td>
<td>@item.EndDate</td>
<td>Aagent Name</td>
<td>Status</td>
</tr>
}
</tbody>