Google-esque Pagination with Razor

Go To StackoverFlow.com

0

I currently have pagination functionality that displays the total page count, but I was wondering how I can have page links to display similar to Google search results? Essentially only showing 10 total links at a time, and if the current page is greater than 7, the first link displayed will be currentPageIndex - 5.

This is the current Razor/html that I have that displays the number of links equal to the total page count:

@for (int i = 0; i < Model.PageCount; i++)
{
    if (Model.CurrentPageIndex == i)
    {
    <li id="page@(i)" class="disabled"><a href="#">@(i + 1)</a></li>
    }
    else
    {
    <li id="page@(i)"><a href="/Search/@(i.ToString() + "?q=" + Model.SearchTerm)">@(i + 1)</a></li>
    }
}
2012-04-04 21:17
by SCS
Have you tried PagedList? It has built-in HTML helpers for rendering the page index links and total count - Michael Hornfeck 2012-04-04 21:26


1

This seems like more of a logic problem than a razor problem.

@{
    int pagesDisplayed = 10;
    int firstPage = Model.CurrentPageIndex - pagesDisplayed / 2;
    if(firstPage < 0){
        firstPage = 0;
    }
}
@for (int i=firstPage; i <= (firstPage + pagesDisplayed); i++){
    if (Model.CurrentPageIndex == i)
    {
    <li id="page@(i)" class="disabled"><a href="#">@(i + 1)</a></li>
    }
    else
    {
    <li id="page@(i)"><a href="/Search/@(i.ToString() + "?q=" + Model.SearchTerm)">@(i + 1)</a></li>
    }
}
2012-04-04 21:31
by devstruck
Ads