views:

1305

answers:

4

Most of the ASP.NET MVC paging solutions I have found by googling look like they get all rows from a database table in the form of a IEnumerable collection, perform some paging conversion on the IEnumerable collection, and then return the results to the view. I want to be able to page on the DB side but still have some paging class to do the page number calculations and HTML generation. Is there a solution out there that does this? Or are the ones i've been looking at do this, but i'm not seeing it because i'm looking at them wrong?

here's what i've been looking at:

+4  A: 

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

Craig Stuntz
so what would the controller action syntax be? everything i've been seeing does something like productRepository.GetAllProducts().ToPagedList(pageIndex, pageSize).
gabe
Yes, that's just right. This converts **the selected page only** to IEnumerable. The paging is done on the DB server via IQueryable. Ditto for Count.
Craig Stuntz
my problem was that i didn't know what IQueryable did (i.e. "deferred execution"). your comments as well as the others pointed me in the right direction for understanding. thx!
gabe
+7  A: 

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.

jvanderh
i'll check it out the sampler.ah, i see. your last sentence greatly enlightened me! thx much!
gabe
I recommend you check out the whole sample, but the link I provided is the chapter that explains in more detail how IQueryable works.
jvanderh
i'm doing that as we comment
gabe
+2  A: 

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.

Sean Reilly
+2  A: 

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

Will Asrari