Wrox Programmer Forums
|
BOOK: Professional ASP.NET MVC 2
This is the forum to discuss the Wrox book Professional ASP.NET MVC 2 by Jon Galloway, Scott Hanselman, Phil Haack, Scott Guthrie, Rob Conery; ISBN: Professional ASP.NET MVC 2
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ASP.NET MVC 2 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 23rd, 2011, 09:00 PM
jam jam is offline
Registered User
 
Join Date: Jun 2011
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default PaginatedList step by step

Step-1:
In my case, Retrieved all the dinners ORDER BY DinnerID in Index action. (Note: Order By is mandatory when you use Skip() and Take() method.)

public ActionResult Index(int Page = 0)
{
const int PageSize = 3;
var alldinners = DinnerRepository.FindAllFinners().OrderBy(d => d.DinnerID);

/* Some code */
}

Step-2:
Create PaginatedList class in the project root (eg: DinnerFinder\ PaginatedList.cs). In my case, DinnerFinder is my project name as the same time default namespace.

namespace DinnerFinder
{
public class PaginatedList<T>:List<T>
{
public int PageIndex { get; private set; }
public int PageSize { get; private set; }
public int PageCount { get; private set; }
public int TotalRec { get; private set; }

public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
{
TotalRec = source.Count();
PageIndex = pageIndex;
PageSize = pageSize;
PageCount =(int) Math.Ceiling(TotalRec / (double)pageSize);

this.AddRange(source.Skip(pageIndex * pageSize).Take(pageSize));
}

public bool HasPreviousPage()
{
if (PageIndex > 0)
{
return true;
}
else
{
return false;
}
}

public bool HasNextPage()
{
if (PageIndex + 1 < PageCount)
{
return true;
}
else
{
return false;
}
}
}
}

Step-3:
Got to the Step-1 part write the following code in the “Some code” section.

var paginatedDinners = new PaginatedList<Dinner>( paginatedDinners, Page, PageSize);
return View("Index", paginatedDinners);


Now the Step-1 looks like as bellow.

public ActionResult Index(int Page = 0)
{
const int PageSize = 3;

var alldinners = DinnerRepository.FindAllFinners().OrderBy(d => d.DinnerID);

var paginatedDinners = new PaginatedList<Dinner>(alldinners, Page, PageSize);

return View("Index", paginatedDinners);
}

Step-4:
Got to the \Views\Dinners\Index.aspx file and update the following part of the code. I my case
Inherits="System.Web.Mvc.ViewPage<DinnerFinder.PaginatedList<DinnerFinder.Models.Din ner>>"

Instead of

Inherits="System.Web.Mvc.ViewPage<DinnerFinder.Models.Dinner>"

Where

DinnerFinder: is the project name/default namespace.
PaginatedList: is the pagination class created and stored in project root
ie. DinnerFinder\PaginatedList.cs

Step-5:
Add the following code snip same as book p-107

<% if (Model.HasPreviousPage) { %>

<%: Html.RouteLink(“<<<”, “Paigination”, new { page=(Model.PageIndex-1) }) %>
<% } %>

<% if (Model.HasNextPage) { %>

<%: Html.RouteLink(“>>>”,“Paigination”, new { page = (Model.PageIndex + 1) })%>

<% } %>

Note: “Paigination” is the new route name that you already created in Global.asax file. If not got to Global.asax and add the following code snip.


routes.MapRoute(
"Paigination",
"Dinners/Page/{page}", // URL with parameter
new { controller="Dinners",action="Index"}
);

Save and run.

Thanks
 
Old June 24th, 2011, 09:40 AM
Friend of Wrox
 
Join Date: Oct 2010
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

Paigination or Pagination ?





Similar Threads
Thread Thread Starter Forum Replies Last Post
need a step by step book angelica ASP.NET 3.5 Basics 5 October 31st, 2008 05:06 AM
AjaxMAIL Step by Step waffa BOOK: Professional Ajax ISBN: 978-0-471-77778-6 3 September 25th, 2006 02:12 PM
How detailed are the step-by-step littleccguy BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 May 18th, 2006 03:45 PM
We Based Database Step By step desireemm SQL Language 1 October 15th, 2005 08:15 PM
Step by step installation peter_budo Beginning PHP 0 August 31st, 2004 12:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.