Reputation: 281
-I am using MVC 3, AdventureworksLT database
-There are lot of records, so the number of pagelinks have gone up to 30.
Here is the code for the view
Upvotes: 0
Views: 676
Reputation: 4596
You'll need to change your loop that generates those links to only show a certain range of page numbers. It is common to provide links to the first and last pages, with links to the pages + or - an arbitrary number of pages away from the current page.
@{
ViewBag.PageRange = 3;
}
<div class = "pagination">
Page:
<a href="@Url.Action("products","home",new{page = 1})">First</a>
@for (int p = 1; p <= ViewBag.TotalPages; p++)
{
if (p >= ViewBag.CurrentPage - ViewBag.PageRange && p <= ViewBag.CurrentPage + ViewBag.PageRange)
{
<a class="@(p==ViewBag.CurrentPage ? "Current" : "")"
href="@Url.Action("products","home",new{page = p})">@p</a>
}
}
<a href="@Url.Action("products","home",new{page = ViewBag.TotalPages})">Last</a>
</div>
Now, what range you want to show and in what way you want to show that range is completely up to you, but this is basically what you're looking for. There are many ways to accomplish this.
Upvotes: 2