gpt4 book ai didi

asp.net - ASP.NET MVC 中的分页

转载 作者:行者123 更新时间:2023-12-02 16:43:13 26 4
gpt4 key购买 nike

我有一个 asp.net 网站,我在其中使用以下代码进行分页:

    PagedDataSource objPds = new PagedDataSource
{
DataSource = ds.Tables[0].DefaultView,
AllowPaging = true,
PageSize = 12
};

对 asp.net-mvc 进行分页的等效最佳方法是什么?我认为这实际上属于 View 代码。

最佳答案

我只需定义一个包含页码的自定义路由:

routes.MapRoute(
"Books", // Route name
"books/{page}", // URL with parameters
new {controller = "Books", action = "List", page = 1}
);

会给你这样的网址:

http://localhost/books/4/

然后在您的 Controller 操作中您将获得此页码:

public BooksController
{
public ActionResult List (int page)
{
/* Retrieve records for the requested page from the database */

return View ();
}
}

所以你的 View 实际上不会知道当前页面。它只会显示提供的记录列表。

您还需要直接在此 View 中或在母版页中生成指向各个页面的链接。

关于asp.net - ASP.NET MVC 中的分页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1254332/

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