ASP.NET MVC 5使用X.PagedList.Mvc进行分页教程

3、\Controllers\UserController.cs 后台代码基本用法:

 
using PagedList;
// GET: User/1
public ActionResult Index(int page = 1)
{
  const int pageSize = 10;
  //List<User> users = (from u in db.Users
  //  orderby u.Id descending
  //  select u).Skip((page - 1) * pageSize).Take(pageSize).ToList();
  //return View(users);
  var iUsers = db.Users.OrderBy(p => p.Id).ToPagedList(page, pageSize);
  return View(iUsers);
}
4、\Views\User\Index.cshtml 前台代码基本用法:

 
@using PagedList
@using PagedList.Mvc
<table class=“table”>
 xxxx
 xxxx
 xxxx
</table>
@Html.PagedListPager((IPagedList)Model, page => Url.Action(“Index”, new { page }))
5、\App_Start\RouteConfig.cs 配置一下:

 
public class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
    routes.MapRoute(
      name: “Default”,
      url: “{controller}/{action}/{page}”,
      defaults: new { controller = “User”, action = “Index”, page = UrlParameter.Optional }
    );
  }
}
复制免费讲解AI专家
public class RouteConfig
{
  public static void RegisterRoutes(RouteCollection routes)
  {
    routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
    routes.MapRoute(
      name: “Default”,
      url: “{controller}/{action}/{page}”,
      defaults: new { controller = “User”, action = “Index”, page = UrlParameter.Optional }
    );
  }
}