I want to use MvcPaging in a PartialView. I have a search page and I need to paginate the results. So far, only the first page from the results appears - when I am trying to go to page 2 I get in the console a 500 error and nothing happens.
Here are the two actions from controller:
public PartialViewResult SearchResults(string lat, string lng, double? dist)
{
if (Request.IsAjaxRequest())
{
string address = Request["address"];
string latitude = lat;
string longitude = lng;
GeoCoordinate coord = new GeoCoordinate(Double.Parse(latitude, CultureInfo.InvariantCulture), Double.Parse(longitude, CultureInfo.InvariantCulture));
IQueryable<Restaurants> near = (from r in _db.Restaurants select r);
results = new List<Restaurants>();
foreach (Restaurants restaurant in near)
{
double latBD = (double)restaurant.Latitude;
double lngDB = (double)restaurant.Longitude;
if (new GeoCoordinate(latBD, lngDB).GetDistanceTo(coord) <= dist * 1000)
{
results.Add(restaurant);
}
}
return PartialView("_SearchResult", results.ToPagedList(0, 2));
}
return PartialView("Search");
}
public ActionResult PaginationAjax(int? page)
{
int currentPageIndex = page.HasValue ? page.Value - 1 : 0;
return PartialView("_SearchResult", results.ToPagedList(currentPageIndex, 2));
}
And the partial view:
@model IPagedList<Restaurants>
@using MvcPaging
foreach (var item in Model)
{
<blockquote>
<h3><a href="Details?Id=@item.Id" target="_blank">@item.Name</a></h3>
</blockquote>
}
<div class="pager">
@Html.Pager(Model.PageSize, Model.PageNumber, Model.TotalItemCount, new AjaxOptions { UpdateTargetId = "searchResults" }).Options(o => o.Action("PaginationAjax"))
</div>
Does anyone has any idea what I am doing wrong? Thanks!
EDIT: Error
GET http://localhost/TakeASeat/Restaurants/PaginationAjax?page=2&X-Requested-With=XMLHttpRequest&_=1431698681795 500 (Internal Server Error)
jQuery.ajaxTransport.send @ jquery-2.1.3.js:8625
jQuery.extend.ajax @ jquery-2.1.3.js:8161
asyncRequest @ jquery.unobtrusive-ajax.js:128
(anonymous function) @ jquery.unobtrusive-ajax.js:138
jQuery.event.dispatch @ jquery-2.1.3.js:4430
jQuery.event.add.elemData.handle @ jquery-2.1.3.js:4116