我使用了Nerddinner教程的分頁示例。但我也想添加頁碼,服用點這樣的:如何使分頁從1而不是0開始
< < < 1 2 3 4 5 6 >>>
下面的代碼,如果我從0開始我的傳呼工作,但不從1.我該如何解決這個問題?
這裏是我的代碼:
PaginatedList.cs
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);
}
}
}
UserController.cs
public ActionResult List(int? page)
{
const int pageSize = 20;
IUserRepository userRepository = new UserRepository();
IQueryable<User> listUsers = userRepository.GetAll();
PaginatedList<User> paginatedUsers = new PaginatedList<User>(listUsers, page ?? 0, pageSize);
return View(paginatedUsers);
}
List.cshtml
個@if (Model.HasPreviousPage)
{
@Html.RouteLink(" Previous ", "PaginatedUsers", new { page = (Model.PageIndex - 1) })
}
@for (int i = 1; i <= Model.TotalPages; i++)
{
@Html.RouteLink(@i.ToString(), "PaginatedUsers", new { page = (@i) })
}
@if (Model.HasNextPage)
{
@Html.RouteLink(" Next ", "PaginatedUsers", new { page = (Model.PageIndex + 1) })
}
這gaves我這個錯誤:'指定的參數已超出有效值的範圍。 參數名稱:skipCount' – Anas
它是標準編碼。檢查括號 – Morteza
我寧願使用這個:'page =(page <1)? 1:頁面? 0;'以確保如果用戶手動更改網址,它仍然有效,但感謝您的線索! – Anas