I was trying to use the pagedList.mvc package to page the results i get from a query and i did it like this in my controller.
public ActionResult AllPosts()
{
int pageSize = 4;
int pageNum = (page ?? 1);
var query = from p in db.Posts
select new ListPostsVM()
{
PostTitle = p.PostTitle,
Author = p.UserProfile.UserName,
DateCreated = p.DateCreated,
CategoryName = p.Category.CategoryName
};
return View(query.ToPagedList(pageNum, pageSize));
}
and in my view i did this
@model IPagedList<Blogger.ViewModels.ListPostsVM>
@using PagedList;
@using PagedList.Mvc;
@{
ViewBag.Title = "AllPosts";
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
<link href="~/Content/PagedList.css" rel="stylesheet" />
<h2>AllPosts</h2>
<div class="allposts">
<table class="table">
<tr>
<th>Title</th>
<th>Author</th>
<th>Category</th>
<th>Date</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@item.PostTitle
<p class="actions">
Edit | Delete | View
</p>
</td>
<td>@item.Author</td>
<td>@item.CategoryName</td>
<td>@item.DateCreated</td>
</tr>
}
</table>
</div>
@Html.PagedListPager(Model, page => Url.Action("Index", new { page = page}), PagedListRenderOptions.OnlyShowFivePagesAtATime)
However when i run build this code and i navigate to the page i get an error saying
An exception of type 'System.NotSupportedException' occurred in System.Data.Entity.dll but was not handled in user code
Additional information: The method 'Skip' is only supported for sorted input in LINQ to Entities. The method 'OrderBy' must be called before the method 'Skip'.
Please how do i solve this?
you are returning wrong type.
I.e. return View(query.ToPagedList(pageNum, pageSize));
You should change your code to:
return View(query.OrderBy(x => x.Author).ToPagedList(pageNumber, pageSize));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With