想知道你在哪里发现并纠正我做错了什么。 我需要实现一些分页,我遇到的问题是索引与页面不同步
EG按下链接“2”第2页我得到与第3页(21-30)等相关的数据......
CHAR
我把一个简单的样本放在一起
模型
Current Result
1 (1-10) correct
2 (21-30) wrong
3 (31-40) wrong
4 (41-50) wrong
etc...
Wanted Result
1 (1-10)
2 (2-20)
3) (21-30)
4 (31-40)
etc...
视图模型
public class Product
{
public string Name { get; set; }
public string Category { get; set; }
}
控制器
public class ProductViewModel
{
public IEnumerable<Product> Items { get; set; }
public Pager Pager { get; set; }
}
寻呼机
public class ProductController : Controller
{
public ActionResult Index(int? page)
{
var allProducts = GetProducts();
int pageIndex;
if (page <= 1)
{
pageIndex = 0;
}
else
{
pageIndex = page ?? 0;
}
int itemsXPage = 10;
var paginatedProducts = allProducts.Skip(pageIndex * itemsXPage).Take(itemsXPage);
var pager = Pager
.Items(allProducts.Count())
.PerPage(10)
.Move(page ?? 0)
.Segment(5)
.Center();
var viewModel = new ProductViewModel
{
Items = paginatedProducts,
Pager = pager
};
return View(viewModel);
}
public IEnumerable<Product> GetProducts()
{
List<Product> products = new List<Product>();
for (int i = 1; i < 150; i++)
{
var product = new Product { Name = "Product" + i, Category = "Shoes" };
products.Add(product);
}
return products;
}
查看
link http://www.superstarcoders.com/blogs/posts/pager-for-asp-net-mvc.aspx
public class Pager : IEnumerable<int>
{
private int _numberOfPages;
private int _skipPages;
private int _takePages;
private int _currentPageIndex;
private int _itemsPerPage;
private Pager()
{
}
private Pager(Pager pager)
{
NumberOfItems = pager.NumberOfItems;
_currentPageIndex = pager._currentPageIndex;
_numberOfPages = pager._numberOfPages;
_takePages = pager._takePages;
_skipPages = pager._skipPages;
_itemsPerPage = pager._itemsPerPage;
}
/// <summary>
/// Creates a pager for the given number of items.
/// </summary>
public static Pager Items(int numberOfItems)
{
return new Pager
{
NumberOfItems = numberOfItems,
_currentPageIndex = 0,
_numberOfPages = 1,
_skipPages = 0,
_takePages = 1,
_itemsPerPage = numberOfItems
};
}
/// <summary>
/// Specifies the number of items per page.
/// </summary>
public Pager PerPage(int itemsPerPage)
{
int numberOfPages = (NumberOfItems + itemsPerPage - 1) / itemsPerPage;
return new Pager(this)
{
_numberOfPages = numberOfPages,
_skipPages = 0,
_takePages = numberOfPages - _currentPageIndex + 1,
_itemsPerPage = itemsPerPage
};
}
/// <summary>
/// Moves the pager to the given page index
/// </summary>
public Pager Move(int pageIndex)
{
return new Pager(this)
{
_currentPageIndex = pageIndex
};
}
/// <summary>
/// Segments the pager so that it will display a maximum number of pages.
/// </summary>
public Pager Segment(int maximum)
{
int count = Math.Min(_numberOfPages, maximum);
return new Pager(this)
{
_takePages = count,
_skipPages = Math.Min(_skipPages, _numberOfPages - count),
};
}
/// <summary>
/// Centers the segment around the current page
/// </summary>
/// <returns></returns>
public Pager Center()
{
int radius = ((_takePages + 1) / 2);
return new Pager(this)
{
_skipPages = Math.Min(Math.Max(_currentPageIndex - radius, 0), _numberOfPages - _takePages)
};
}
public IEnumerator<int> GetEnumerator()
{
return Enumerable.Range(_skipPages + 1, _takePages).GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public bool IsPaged { get { return NumberOfItems > _itemsPerPage; } }
public int NumberOfPages { get { return _numberOfPages; } }
public bool IsUnpaged { get { return _numberOfPages == 1; } }
public int CurrentPageIndex { get { return _currentPageIndex; } }
public int NextPageIndex { get { return _currentPageIndex + 1; } }
public int LastPageIndex { get { return _numberOfPages; } }
public int FirstPageIndex { get { return 1; } }
public bool HasNextPage { get { return _currentPageIndex < _numberOfPages && _numberOfPages > 1; } }
public bool HasPreviousPage { get { return _currentPageIndex > 1 && _numberOfPages > 1; } }
public int PreviousPageIndex { get { return _currentPageIndex - 1; } }
public bool IsSegmented { get { return _skipPages > 0 || _skipPages + 1 + _takePages < _numberOfPages; } }
public bool IsEmpty { get { return _numberOfPages < 1; } }
public bool IsFirstSegment { get { return _skipPages == 0; } }
public bool IsLastSegment { get { return _skipPages + 1 + _takePages >= _numberOfPages; } }
public int ItemStart { get { return CurrentPageIndex * _itemsPerPage + 1; } }
public int ItemEnd
{
get { return Math.Min(CurrentPageIndex * _itemsPerPage + _itemsPerPage, NumberOfItems); }
}
public int NumberOfItems { get; set; }
}
你能发现我做错了吗?
答案 0 :(得分:0)
这是错误的
if (page <= 1)
{
pageIndex = 0;
}
else
{
pageIndex = page ?? 0;
}
只是递减你的页码而不是这个,看起来你的页面var是基于1但索引是基于0。
因为你有1个进入第一页获得页面索引0,但如果你传入2它保持2,所以没有办法得到实际的pageindex为1