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
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