Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can try my pager HTML helper:</p> <pre><code>using System; using System.Text; using System.Web.Mvc; using System.Web.Routing; namespace System.Web.Mvc { public class Pager { private ViewContext viewContext; private readonly int pageSize; private readonly int currentPage; private readonly int totalItemCount; private readonly RouteValueDictionary linkWithoutPageValuesDictionary; public Pager(ViewContext viewContext, int pageSize, int currentPage, int totalItemCount, RouteValueDictionary valuesDictionary) { this.viewContext = viewContext; this.pageSize = pageSize; this.currentPage = currentPage; this.totalItemCount = totalItemCount; this.linkWithoutPageValuesDictionary = valuesDictionary; } public string RenderHtml() { int pageCount = (int)Math.Ceiling(this.totalItemCount / (double)this.pageSize); int nrOfPagesToDisplay = 8; var sb = new StringBuilder(); sb.Append("&lt;ul class=\"pagination\"&gt;"); // Previous if (this.currentPage &gt; 1) { sb.Append(string.Format("&lt;li class=\"prev\"&gt;&lt;a href=\"{0}\"&gt;«&lt;/a&gt;&lt;/li&gt;", Route(this.currentPage - 1))); } else { sb.Append("&lt;li class=\"prev disabled\"&gt;&lt;span&gt;«&lt;/span&gt;&lt;/li&gt;"); } int start = 1; int end = pageCount; if (pageCount &gt; nrOfPagesToDisplay) { int middle = (int)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1; int below = (this.currentPage - middle); int above = (this.currentPage + middle); if (below &lt; 4) { above = nrOfPagesToDisplay; below = 1; } else if (above &gt; (pageCount - 4)) { above = pageCount; below = (pageCount - nrOfPagesToDisplay); } start = below; end = above; } if (start &gt; 3) { sb.Append(GeneratePageLink("1", 1)); sb.Append(GeneratePageLink("2", 2)); sb.Append("&lt;li class=\"more\"&gt;...&lt;/li&gt;"); } for (int i = start; i &lt;= end; i++) { if (i == this.currentPage) { sb.Append(string.Format("&lt;li class=\"page selected\"&gt;&lt;span&gt;{1}&lt;/span&gt;&lt;/li&gt;", Route(i),i)); } else { sb.Append(GeneratePageLink(i.ToString(), i)); } } if (end &lt; (pageCount - 3)) { sb.Append("&lt;li class=\"more\"&gt;...&lt;/li&gt;"); sb.Append(GeneratePageLink((pageCount - 1).ToString(), pageCount - 1)); sb.Append(GeneratePageLink(pageCount.ToString(), pageCount)); } // Next if (this.currentPage &lt; pageCount) { sb.Append(string.Format("&lt;li class=\"next\"&gt;&lt;a href=\"{0}\"&gt;»&lt;/a&gt;&lt;/li&gt;", Route(this.currentPage + 1))); } else { sb.Append("&lt;li class=\"next disabled\"&gt;&lt;span&gt;»&lt;/span&gt;&lt;/li&gt;"); } sb.Append("&lt;/ul&gt;"); return sb.ToString(); } private string Route(int pageNumber) { var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary); pageLinkValueDictionary.Add("page", pageNumber); var virtualPathData = RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary); return virtualPathData.VirtualPath; } private string GeneratePageLink(string linkText, int pageNumber) { var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary); pageLinkValueDictionary.Add("page", pageNumber); var virtualPathData = RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary); if (virtualPathData != null) { string linkFormat = "&lt;li class=\"page\"&gt;&lt;a href=\"{0}\"&gt;{1}&lt;/a&gt;&lt;/li&gt;"; return String.Format(linkFormat, virtualPathData.VirtualPath, linkText); } else { return null; } } } } </code></pre> <p>How to use:</p> <pre><code>&lt;%= Html.Pager(10, (Request["page"].IsNotNull() ? Request["page"].ToInt() : 1), ViewData["Total"].ToInt(), new { category = Request["category"], alphabet = Request["alphabet"] })%&gt; </code></pre> <p>And Controller implementation looks like this:</p> <pre><code>public ActionResult Index(string page, string category, string alphabet) { ..... ViewData["Total"] = model.Count(); return View(model.ToPagedList((page.IsNotNull() ? page.ToInt() - 1 : 0), 10)); } </code></pre> <p>And finally output:</p> <p><a href="http://clip2net.com/clip/m12007/1247389672-clip-1kb.png" rel="nofollow noreferrer">pager http://clip2net.com/clip/m12007/1247389672-clip-1kb.png</a> </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload