Reputation: 39
I am using Asp.NET MVC4 with Razor.
I want to show a list of items/entries (coming from a database) over several pages where the user can navigate to the next page or a specific page and each page shows e.g. 30 entries. It should be similar to the way the questions are structured in stackoverflow with this little page navigator at the bottom. How would I do that? I would guess that an example already exists but I have not found any. Right now I show all my items on one page using a partial view in my index-view:
@foreach (var item in Model) {
@Html.Partial("_ItemInList",item)
I could probably only show the first 30 items but how do I store on which page I am and make the links so that the user can navigate to the other pages?
Upvotes: 0
Views: 104
Reputation: 136
There's an awful lot of ready made pagination controls available if you're looking for a quick result... but you're probably better off rolling your own. You can restrict your result set to a given page size in your data access layer using Linq and populate your model with single page (plus totals, page number etc...).
Depending on how you've structured your application though, you may want to push paging down to the database level and limit the result set size there rather than higher up the stack.
Upvotes: 0
Reputation: 1038870
You could use a pagination control. There are many such components. For example you may take a look at MvcPaging
hosted on GitHub.
Upvotes: 1