@html.pagedlistpager post 提交,PagedList.Core.Mvc PagedListPager Html extension in .Net Core is not th...

本文介绍了如何在.NET Core 3.0中使用X.PagedList库实现高效分页,包括在Controller、View和Repository中的实践技巧,以及如何结合搜索和查询参数进行优化。务必避免不必要的数据加载,提高性能。
摘要由CSDN通过智能技术生成

To use PagedList in .Net Core 3.0 we will use https://github.com/dncuug/X.PagedList

Open NuGet Package Manager Install X.PagedList.Mvc.Core

_ViewImports.cshtml

@using X.PagedList.Mvc.Core;

@using X.PagedList;

@using X.PagedList.Mvc.Common

BrandController.cs

public ActionResult Index(int? page)

{

var pageNumber = page ?? 1;

var pageSize = 10; //Show 10 rows every time

var brands = this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);

return View(brands);

}

Index.cshtml

Change

@model IEnumerable

to

@model IPagedList

Change every

@Html.DisplayNameFor(model => model.BrandName)

To

@Html.DisplayNameFor(model => model.First().BrandName)

At the the end of your html table add paging numbers like

@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index",

new

{

page

}),

new PagedListRenderOptionsBase

{

LiElementClasses = new string[] { "page-item" },

PageClasses = new string[] { "page-link" },

Display = PagedListDisplayMode.IfNeeded

})

If you make search or using other querystring you have to do something like that:

BrandController.cs

public ActionResult Index(string search,int? page)

{

var pageNumber = page ?? 1;

var pageSize = 10; //Show 10 rows every time

var brands = this._UoW.BrandsRepository.GetAll().Where(b =>

b.BrandName.Contains(search) ||

search == null).ToPagedList(pageNumber, pageSize);

return View(brands);

}

Index.cshtml

At the the end of your html table add paging numbers like

@Html.PagedListPager((IPagedList)Model, page => Url.Action("Index",

new

{

page,

search = Context.Request.Query["search"]

}),

new PagedListRenderOptionsBase

{

LiElementClasses = new string[] { "page-item" },

PageClasses = new string[] { "page-link" },

Display = PagedListDisplayMode.IfNeeded

})

For best performance use

.ToPagedList with IQueryable

so you will return only 10 rows from the database every time not the whole rows

Use

public IQueryable GetAll()

{

return this._DbSet;

}

with

this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);

Do not use .ToList()

this._UoW.BrandsRepository.GetAll().ToList().ToPagedList(pageNumber,pageSize);

Do not use

public IEnumerable GetAll()

{

return this._DbSet;

}

with

this._UoW.BrandsRepository.GetAll().ToPagedList(pageNumber,pageSize);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值