Note that there are some explanatory texts on larger screens.

plurals
  1. PO(Homework) MVC Pagination Help
    primarykey
    data
    text
    <p>I'm attempting to put together a very simple application using ASP.NET MVC that shows news articles and paginates them. I'm sort of half-way there but need some help sorting out the pagination and getting it to work with the search query.</p> <p>Here is my HomeController:</p> <pre><code>public ActionResult Index(String query, int? page) { // limit the number of articles per page const int pageSize = 4; // build the query var ArticleQuery = from a in _db.ArticleSet select a; // check if their is a query if (!string.IsNullOrEmpty(query)) { ArticleQuery = ArticleQuery.Where(a =&gt; a.headline.Contains(query)); } // orders the articles var OrderedArticles = ArticleQuery.OrderByDescending(a =&gt; a.posted); // takes the ordered articles and paginates them //var paginatedArticles = new PaginatedList(OrderedArticles.Skip((page ?? 0) * pageSize).Take(pageSize), page ?? 0, pageSize); var paginatedArticles = new PaginatedList&lt;Article&gt;(OrderedArticles, page ?? 0, pageSize); // return the paginated articles to the view return View(paginatedArticles); } </code></pre> <p>The idea is that the Controller shows 4 items per page will order them by date. Here is the View I have for the Index method:</p> <pre><code>&lt;ul id="pagination"&gt; &lt;% if (Model.PreviousPage) { %&gt; &lt;li&gt;&lt;%= Html.ActionLink("&lt;&lt; First Page", "Index")%&gt;&lt;/li&gt; &lt;li&gt;&lt;%= Html.ActionLink("&lt;&lt; Previous Page", "Index", new { page=(Model.PageIndex-1) }) %&gt;&lt;/li&gt; &lt;% } %&gt; &lt;% if (Model.NextPage) { %&gt; &lt;li&gt;&lt;%= Html.ActionLink("Next Page &gt;&gt;", "Index", new { page = (Model.PageIndex + 1) })%&gt;&lt;/li&gt; &lt;li&gt;&lt;%= Html.ActionLink("Last Page &gt;&gt;", "Index", new { page = (Model.TotalPages - 1) })%&gt;&lt;/li&gt; &lt;% } %&gt; &lt;/ul&gt; </code></pre> <p>The idea is that these two pagination links will only show if the conditions are true.</p> <p>Finally here is the PaginatedList class for the pager:</p> <pre><code> using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace NewsApp.Models { public class PaginatedList&lt;T&gt; : List&lt;T&gt; { 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.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><strong>NOTE: I' don't want to use any 3rd party components like MVCContrib etc as this is for a University assignment so would defeat the purpose.</strong></p> <p>The pagination works fine now but when I do a search and have e.g. /?query=test I want to be able to page the results, at the moment they get lost :/</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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