Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Professional ASP.NET MVC 2
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
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
  #2 (permalink)  
Old June 24th, 2011, 09:40 AM
Friend of Wrox
Points: 539, Level: 8
Points: 539, Level: 8 Points: 539, Level: 8 Points: 539, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Location: Seattle
Posts: 106
Thanks: 1
Thanked 17 Times in 17 Posts
Default

Paigination or Pagination ?
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 08:24 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.