Category フィルターと ItemsPerPage に基づいてフィルター処理された製品のリストを表示しようとしていますが、PagedList で使用しようとすると問題が発生します。
PagedList の専門知識を持つ人が、自分でページネーション コードを記述する必要があるか、または PagedList を使用して必要な結果を得る方法があるかどうかをアドバイスしてくれます。
LINQ の Skip & Take 関数を使用して、現在のページに表示する必要がある行数のみを取得していますが、フィルターの合計数に基づいてページを表示するページング リンクが必要です。
例:私の検索フィルターは 50 件の結果を見つけますが、1 ページあたりの行数は 10 項目なので、LINQ の Skip() & Take() を使用して 10 行のみを取得します。View.cshtml にページ リンク<< 1 |を表示する必要があります。2 | 3 | 4 | 5 >> 現在、デフォルトの PagedList では、<< 1 >>しか取得できません。1 ページしか表示されない理由はわかっていますが、サブセットのみを取得しながら正しい数のページ リンクを表示する方法を知りたかっただけです。結果の。
**私の目標は、最適化されたクエリをデータベースに書き込んで、Web ページの応答パフォーマンスが高速になるようにすることです。
アクション メソッドのコードは次のようになります。コードは正しい結果を取得していますが、必要に応じてページネーションが機能していません。
public ViewResult List(int page =1, string category =null)
{
if (category != null) this.CurrentCategory = category;
var products = repository.Products
.Where(p => this.CurrentCategory == null || p.Category == this.CurrentCategory)
.OrderBy(p => p.ProductID)
.Skip((page -1) * PageSize)
.Take(PageSize);
return View(products.ToList().ToPagedList(page, PageSize));
}
ページネーションを扱う View のコード スニペットを次に示します。プロジェクトの Github サイトを調べましたが、カスタム ページを提供する拡張メソッドが見つかりませんでした。「ページあたりのアイテム」と @Model の製品の Count() に基づいてページ数のみをレンダリングすると思います。
@model IPagedList<Product>
//foreach loop that renders the @Model
//Code that displays the pagination using PagedList
<div style="text-align:center">
@Html.PagedListPager(Model, page => Url.Action("List", new { page = page, category = ViewBag.CurrentCategory }), PagedListRenderOptions.OnlyShowFivePagesAtATime
)
</div>