我试图使用pagedList.mvc包来查询我从查询得到的结果,我在我的控制器中这样做.
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));
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我做到了这一点
@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)
Run Code Online (Sandbox Code Playgroud)
但是当我运行构建此代码并导航到页面时,我收到错误说
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'.
Run Code Online (Sandbox Code Playgroud)
请问我该如何解决这个问题?
您返回的类型错误。
IEreturn View(query.ToPagedList(pageNum, pageSize));
您应该将代码更改为:
return View(query.OrderBy(x => x.Author).ToPagedList(pageNumber, pageSize));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1050 次 |
| 最近记录: |