Pagination - from nerddinne rasp.net mvc application

R

RichB

I am currently working my way through the nerddinner application and chapter
from the asp.net mvc 1.0 book. I am happy with all of the concepts so far,
but have an issue with the Pagination of upcoming dinners.

I have followed the chapter though and whilst I have changed some of the
object names, I have kept to the application as documented within the
chapter.

My issue is with a line of code in the PaginatedList Helper class:

public class PaginatedList<T> : List<T>
{

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<T> 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 > 0);
}
}

public bool HasNextPage
{
get
{
return (PageIndex + 1 < TotalPages);
}
}
}


When I run the application with a breakpoint at the line:
this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));

Prior to the breakpoint I can see in the locals inspector that source
contains the following (relevant extract):

- Results View Expanding the Results
View will enumerate the IEnumerable

- [0] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 5 int

- [1] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 7 int

- [2] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 8 int

- [3] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 9 int

- [4] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 10 int

- [5] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 11 int

- [6] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 12 int

- [7] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 13 int

- [8] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 14 int



I agree with this data, according to the query executed and the database.
After the line is executed (pageIndex = 0, pageSize = 5) this data remains
the same, however the PaginatedList object is shown as follows:

this:

- [0] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 11 int

- [1] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 10 int

- [2] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 9 int

- [3] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 8 int

- [4] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 7 int


This shows the correct number of records, however there order has been
reversed and does not include the first record.

Furthermore if I then try to move the the second page of the set I get the
following results (again source remains unchanged):

- [0] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 11 int

- [1] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 12 int

- [2] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 13 int

- [3] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 14 int

This would seem to be correct.

Can anyone shed any light onto what might be happening here?

Thanks,
Richard
 
G

Gregory A. Beamer

I am currently working my way through the nerddinner application and chapter
from the asp.net mvc 1.0 book. I am happy with all of the concepts so far,
but have an issue with the Pagination of upcoming dinners.

I have followed the chapter though and whilst I have changed some of the
object names, I have kept to the application as documented within the
chapter.

My issue is with a line of code in the PaginatedList Helper class:

public class PaginatedList<T> : List<T>
{

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<T> 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 > 0);
}
}

public bool HasNextPage
{
get
{
return (PageIndex + 1 < TotalPages);
}
}
}


When I run the application with a breakpoint at the line:
this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));

Prior to the breakpoint I can see in the locals inspector that source
contains the following (relevant extract):

- Results View Expanding the Results
View will enumerate the IEnumerable

- [0] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 5 int

- [1] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 7 int

- [2] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 8 int

- [3] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 9 int

- [4] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 10 int

- [5] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 11 int

- [6] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 12 int

- [7] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 13 int

- [8] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 14 int



I agree with this data, according to the query executed and the database.
After the line is executed (pageIndex = 0, pageSize = 5) this data remains
the same, however the PaginatedList object is shown as follows:

this:

- [0] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 11 int

- [1] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 10 int

- [2] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 9 int

- [3] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 8 int

- [4] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 7 int


This shows the correct number of records, however there order has been
reversed and does not include the first record.

Furthermore if I then try to move the the second page of the set I get the
following results (again source remains unchanged):

- [0] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 11 int

- [1] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 12 int

- [2] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 13 int

- [3] {EventsManager.Models.Event}
EventsManager.Models.Event

_EventID 14 int

This would seem to be correct.

Can anyone shed any light onto what might be happening here?


IMO, the pagination needs to be changed, as you go from

List
to
List/#

I understand why they did it, but I believe it should be List and
List/1 are both page 1.

As for why you are seeing issues, it goes down to the way things are
retrieved from the database. Put Profiler on SQL Server and watch the
queries and you will see why.

I had thought about reworking this bit, but I am not using this type
of functionality in my site ... yet. When I get around to lists, i
will exmaine it, but you will probably have a solution by then.

Peace and Grace,

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

*************************************************
| Think outside the box! |
*************************************************
 
A

Allen Chen [MSFT]

Hi Richard,

I could not reproduce this issue on my side. Could you send me a demo
project that can reproduce it? My email is (e-mail address removed)
update here after sending the project in case I missed that email.

Regards,
Allen Chen
Microsoft Online Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
R

RichB

Allen,

Thanks, for your response. in messing around with the code over the weekend
I seems to have completely screwed the database and some of the code. I have
therefore rebuilt the db and app from scratch, and cannot now recreate the
issue below.

I feel that I had some sort of db corruption (I cannot now read the db
atall), but cannot be sure. However it now functions exactly as expected.

Many Thanks for your help.
Richard
 
A

Allen Chen [MSFT]

Thanks, for your response. in messing around with the code over the
weekend
I seems to have completely screwed the database and some of the code. I have
therefore rebuilt the db and app from scratch, and cannot now recreate the
issue below.
I feel that I had some sort of db corruption (I cannot now read the db
atall), but cannot be sure. However it now functions exactly as expected.
Many Thanks for your help.
Richard

No problem, Richard. Glad to know you've solved this issue.

Thank you for using our Newsgroup Support Service!

Regards,
Allen Chen
Microsoft Online Community Support


=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).
=================================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top