Is there a paging solution for ASP.NET MVC that does paging in the database?

回眸只為那壹抹淺笑 提交于 2019-12-02 20:52:47
jvanderh

Look at the Gu's Nerdinner sample.

var upcomingDinners = dinnerRepository.FindUpcomingDinners();  
var paginatedDinners = upcomingDinners.Skip(10).Take(20).ToList(); 

Even though FindUpcomingDinners() gets all the upcoming dinners, the query isn't executed at the database until you call ToList() in the next line. And that is after you Skip 10 rows and only get
the next 20.

You're wrong. PagedList will do it on the DB server, as it has IQueryable extensions.

ScottGu has a very nice multi-part blog series on using LINQ in Asp.Net (including MVC). I recommend reading the entire series starting at Part 1, but Part 3 covers exectly what you're looking for -- the section titled "Paging our Query Results" specifically deals with paging in the database.

wouldn't it be more efficient to implement a stored procedure that takes @StartPage and @PageSize parameters?

this way you are only retrieving the subset of data that is actually being used

just have an out parameter called totalCount or something similar so that you know how many page links to create and each link onclick event will pass the page number to a javascript function that will asynchronously load the div or other HTML element with more data

easy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!