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