gpt4 book ai didi

c# - 使用 PagedList.Mvc 的错误

转载 作者:太空狗 更新时间:2023-10-29 23:38:31 27 4
gpt4 key购买 nike

我试图使用 pagedList.mvc 包来分页我从查询中获得的结果,我在我的 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));
}

在我看来我做到了

@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)

但是,当我运行构建此代码并导航到该页面时,出现错误提示

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'.

请问我该如何解决?

最佳答案

您返回的类型错误。

return View(query.ToPagedList(pageNum, pageSize));

您应该将代码更改为:

return View(query.OrderBy(x => x.Author).ToPagedList(pageNumber, pageSize));

关于c# - 使用 PagedList.Mvc 的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27564267/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com