Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I set the number of pages shown in a search for ASP.NET MVC3
    primarykey
    data
    text
    <p>How can I shorten the number of pages shown in a list?</p> <p>Currently when searching, the results page will show every number in the list e.g First Page 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, etc. to the Last Page. Some search results can have up to 40 pages so it looks a bit messy.</p> <p>How can I change the code to show the pages like this e.g First Page 1, 2, 3,...9, 10. Last Page.</p> <p>When page three is selected the list would show e.g First Page 3, 4, 5,...9, 10. Last Page.</p> <p>I am working with .NET MVC3. This is the code being used to generate the view</p> <pre><code> &lt;%@ Control Language="C#"Inherits="System.Web.Mvc.ViewUserControl&lt;MyApp.Web.Extensions.PaginatedList&lt;MyApp.Data.Product&gt;&gt;" %&gt; &lt;div style="text-align: center; margin-bottom: 5px;"&gt; &lt;ul id="paginator"&gt; &lt;% if (Model.HasPreviousPage) { %&gt; &lt;li&gt; &lt;%= Html.RouteLink("First Page", new { namespaces = "Administrator", controller = "Products", action = "ViewAll", page = 0 })%&gt;&lt;/li&gt; &lt;% } %&gt; &lt;% else %&gt; &lt;% { %&gt; &lt;li&gt;First Page&lt;/li&gt; &lt;% } %&gt; &lt;%-- Show all the pages here in a list --%&gt; &lt;% for (int i = 0; i &lt; Model.TotalPages; i++) %&gt; &lt;% { %&gt; &lt;% if (i == Model.PageIndex) %&gt; &lt;% { %&gt; &lt;li&gt; &lt;%: i+1 %&gt;&lt;/li&gt; &lt;% } %&gt; &lt;% else %&gt; &lt;% { %&gt; &lt;li&gt; &lt;%= Html.RouteLink((i + 1).ToString(), new { namespaces = "Administrator", controller = "Products", action = "ViewAll", page = i })%&gt; &lt;/li&gt; &lt;% } %&gt; &lt;% } %&gt; &lt;% if (Model.HasNextPage) { %&gt; &lt;li&gt; &lt;%= Html.RouteLink("Last Page", new { namespaces = "Administrator", controller = "Products", action = "ViewAll", page = Model.TotalPages - 1 })%&gt;&lt;/li&gt; &lt;% } %&gt; &lt;% else %&gt; &lt;% { %&gt; &lt;li&gt;Last Page&lt;/li&gt; &lt;% } %&gt; &lt;/ul&gt; &lt;/div&gt; </code></pre> <p>This code for the class</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MyApp.Web.Extensions { public class PaginatedList&lt;T&gt; : List&lt;T&gt; where T : MyApp.Data.Product { public int PageIndex { get; private set; } public int PageSize { get; private set; } public int TotalCount { get; private set; } public int TotalPages { get; private set; } public PaginatedList(IQueryable&lt;T&gt; source, int pageIndex, int pageSize) { PageIndex = pageIndex; PageSize = pageSize; TotalCount = source.Count(); TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize); this.AddRange(source.OrderBy(t =&gt; t.CreatedDate).OrderByDescending(t =&gt; t.CreatedDate).Skip(PageIndex * PageSize).Take(PageSize)); } public bool HasPreviousPage { get { return (PageIndex &gt; 0); } } public bool HasNextPage { get { return (PageIndex + 1 &lt; TotalPages); } } } } </code></pre> <p>If additional information is needed please leave a comment. Also thank you in advance for any help offered.</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.
 

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