I tried to use the pagedList.mvc package to output the results obtained from the query, and did 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 opinion I did it
@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 the assembly of this code, and I go to the page, I get an error
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'.
How do i solve this?
source
share