views:

1552

answers:

1

This started as a question, but turned into a solution as I did some experimenting! So I thought I would share this with you all. My question WAS:

How to use MvcContrib.Pagination without using MvcContrib.Grid View?

My answer is below...

+3  A: 

I am building a Help Desk Ticketing System (I am kind of a C# newbie - got many pointers from NerdDinner) and I wish to use some sort of paging library to help with the view. I found MvcContrib.Pagination and I got it to work for a view. My view does NOT use MvcContrib.Grid because it is custom.

Scaled down version of my view List.aspx :

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MyProject.Areas.HelpDesk.Models.hd_Ticket>>" %>
<%@ Import Namespace="MyProject.Areas.HelpDesk.Controllers" %>
<%@ Import Namespace="MvcContrib.Pagination" %>

<h2>Help Desk Tickets (showing <%= Model.Count() %> of <%= ViewData["totalItems"] %>)</h2>     

<% foreach (var item in Model) { %>
    <h3><%= Html.Encode(item.Subject)%></h3>
<% } %>

<p><%= Html.Pager((IPagination)Model)%></p>

My controller (part) TicketController.cs :

TicketRepository ticketRepository = new TicketRepository();

public ActionResult List(int? page, int? pageSize)
{
    IPagination<hd_Ticket> tickets = null;

    int dPageSize = 50;
    int totalItems;

    tickets = ticketRepository.GetTickets().ToList().AsPagination(page ?? 1, pageSize ?? dPageSize);
    ViewData["totalItems"] = tickets.TotalItems;

    return View("List", tickets);
}

I am using the repository pattern which is returning the results as IQueryable. Here is part of the TicketRepository.cs file:

public class TicketRepository
{
    private HelpDeskDataContext db = new HelpDeskDataContext();

    public IQueryable<hd_Ticket> FindAllTickets()
    {
     return from ticket in db.hd_Tickets
         orderby ticket.CreatedDate descending
         select ticket;
    }
}

All this may be trivial to some, but if someone like me is trying to learn C# and ASP.NET MVC and paging, then this may be useful. I recommend newbies to do the NerdDinner tutorial found at:

http://nerddinnerbook.s3.amazonaws.com/Intro.htm

:)

robnardo
Works fine for me... :)
DucDigital
You shouldn't call ToList() before AsPagination(). ToList() builds a list of all the items in your query, so you're still getting everything from the repository even if later you're only displaying a subset of that on the page.
dmnd