且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 ASP.NET MVC 中进行分页?

更新时间:2022-02-11 21:14:47

那么,数据源是什么?您的操作可能需要一些默认参数,即

Well, what is the data source? Your action could take a few defaulted arguments, i.e.

ActionResult Search(string query, int startIndex, int pageSize) {...}

在路由设置中默认设置为 startIndex 为 0,pageSize 为(例如)20:

defaulted in the routes setup so that startIndex is 0 and pageSize is (say) 20:

        routes.MapRoute("Search", "Search/{query}/{startIndex}",
                        new
                        {
                            controller = "Home", action = "Search",
                            startIndex = 0, pageSize = 20
                        });

要拆分提要,您可以很容易地使用 LINQ:

To split the feed, you can use LINQ quite easily:

var page = source.Skip(startIndex).Take(pageSize);

(或者如果你使用pageNumber"而不是startIndex"进行乘法)

(or do a multiplication if you use "pageNumber" rather than "startIndex")

使用 LINQ-toSQL、EF 等 - 这也应该组合"到数据库中.

With LINQ-toSQL, EF, etc - this should "compose" down to the database, too.

然后您应该能够使用操作链接到下一页(等):

You should then be able to use action-links to the next page (etc):

<%=Html.ActionLink("next page", "Search", new {
                query, startIndex = startIndex + pageSize, pageSize }) %>