mc_dev
mc_dev

Reputation: 281

How to display lesser number of page links?

-I am using MVC 3, AdventureworksLT database

-There are lot of records, so the number of pagelinks have gone up to 30. enter image description here

Upvotes: 0

Views: 676

Answers (1)

Jim Skerritt
Jim Skerritt

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>&nbsp;

    @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>
        }
    }

    &nbsp;<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

Related Questions