2014-12-19 5 views
4

pagedList.mvc 패키지를 사용하여 쿼리에서 결과를 페이지화하려고했는데 컨트롤러에서이 작업을 수행했습니다. 나는이 코드를 구축 실행하고 난 페이지를 탐색 할 때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)); 
    } 

내보기에 내가이

@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.OrderBy(x => x.PostTitle).ToPagedList(pageNum, pageSize)); 

답변

2

:

0

this link에 따르면, 다음에 return 진술을 업데이트해야합니다.

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

당신은 당신의 코드를 변경해야합니다

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