gpt4 book ai didi

asp.net-mvc - 一种在 aspnetboilerplate 中进行分页和排序的简单方法

转载 作者:行者123 更新时间:2023-12-02 20:33:21 26 4
gpt4 key购买 nike

是否有任何简单的方法可以在 ASP.NET 样板解决方案中的表示层中实现分页,例如 PagedList.Mvc Nuget 包中的 PagedList

我从 Nuget gallery 安装了 PagedList.Mvc 包,并将我的 Index 方法更改为:

 public async Task<ActionResult> Index(int? page)
{
var pageIndex = (page ?? 1) - 1; //NewsService expects a 0 for the first page
var pageSize = 3;
int totalUserCount; // will be set by call to GetAllNews due to _out_ paramter :-|
var newsResult = await _newsAppService.GetAllNews(new PagedResultRequestDto { MaxResultCount = pageSize });
totalUserCount = newsResult.TotalCount;

var news = newsResult.Items;
var newsAsIPagedList = new StaticPagedList<NewsDto>(news, pageIndex + 1, pageSize, totalUserCount);

ViewBag.OnePageOfNews = newsAsIPagedList;


var model = new NewsListViewModel
{
News = news
};

return View(model);
}

在 View 中,我将其添加到新闻表下:

@Html.PagedListPager((IPagedList)ViewBag.OnePageOfNews, page => Url.Action("Index", new { page }))

但是当我单击数字(例如数字 2 或 3)时,表格会刷新但会更改数据。

最佳答案

ASP.NET Boilerplate 不提供此功能,但按照您的建议使用 PagedList.Mvc 很简单。

很容易修改UsersController.Index使用manual paging :

public async Task<ActionResult> Index(int? page)
{
var pageIndex = (page ?? 1) - 1;
var pageSize = 10;
int totalUserCount;

var skipCount = pageIndex * pageSize;
var pagedResult = await _userAppService.GetAll(new PagedResultRequestDto { MaxResultCount = pageSize, SkipCount = skipCount });
totalUserCount = pagedResult.TotalCount;

var users = pagedResult.Items;
var usersAsIPagedList = new StaticPagedList<UserDto>(users, pageIndex + 1, pageSize, totalUserCount);

ViewBag.OnePageOfUsers = usersAsIPagedList;

// Code below unchanged
var roles = (await _userAppService.GetRoles()).Items;
var model = new UserListViewModel
{
Users = users,
Roles = roles
};
return View(model);
}

关于asp.net-mvc - 一种在 aspnetboilerplate 中进行分页和排序的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47920568/

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