پیاده سازی کتابخانه PagedList.MVC برای صفحه بندی اطلاعات در ASP.NET MVC
نویسنده: عثمان رحیمی
تاریخ: ۱۳۹۵/۰۶/۰۶ ۲۰:۱۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
Install-Package PagedList.Mvc
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public string Body { get; set; }
} public static class PostService
{
public static IEnumerable<Post> posts = new List<Post>() {
new Post{Id=1,Title="Title 1",Body="Body 1"},
new Post{Id=2,Title="Title 2",Body="Body 2"},
new Post{Id=3,Title="Title 3",Body="Body 3"},
new Post{Id=4,Title="Title 4",Body="Body 4"},
new Post{Id=5,Title="Title 5",Body="Body 5"},
new Post{Id=6,Title="Title 6",Body="Body 6"},
new Post{Id=7,Title="Title 7",Body="Body 7"},
new Post{Id=8,Title="Title 8",Body="Body 8"},
new Post{Id=9,Title="Title 9",Body="Body 9"},
new Post{Id=10,Title="Title 10",Body="Body 10"},
new Post{Id=11,Title="Title 11",Body="Body 11"},
new Post{Id=12,Title="Title 12",Body="Body 12"},
};
public static IEnumerable<Post> GetAll()
{
return posts
.OrderBy(row => row.Id);
}
} public ActionResult Index(int? page)
{
var pageNumber = page ?? 1;
var posts = PostService.GetAll();
var result = posts.ToPagedList(pageNumber, 10);
ViewBag.posts = result;
return View();
} @using PagedList.Mvc;
@using PagedList;
<link href="/Content/PagedList.css" rel="stylesheet" type="text/css" />
<h2>List of posts</h2>
<ul>
@foreach (var post in ViewBag.posts)
{
<li>@post.Title</li>
}
</ul>
@Html.PagedListPager((IPagedList)ViewBag.posts, page => Url.Action("Index", new { page })) public static IEnumerable<Post> GetAll(int page, int recordsPerPage,out int totalCount)
{
totalCount = posts.Count();
return posts
.OrderBy(row => row.Id).Skip(page * recordsPerPage).Take(recordsPerPage); // in real projects change like this .skip(()=>resultforSkip).Take(()=>recordsPerPage )
} public ActionResult Index(int? page)
{
var pageIndex = (page ?? 1) - 1;
var pageSize = 10;
int totalPostCount;
var posts = PostService.GetAll(pageIndex, pageSize, out totalPostCount);
var result = new StaticPagedList<Post>(posts, pageIndex + 1, pageSize, totalPostCount);
ViewBag.posts = result;
return View();
} http://localhost:53192/?page=2
routes.MapRoute(
name: "paging",
url: "{controller}/{action}/{page}",
defaults: new { controller = "Home", action = "Index", page = UrlParameter.Optional }
); @model PagedList.IPagedList <pagedListmvc.Models.Post>
@foreach (var post in Model)
{
<li>@post.Title</li>
} @Html.PagedListPager( myList, page => Url.Action("Index", new { page = page, tag= "mvc" }) ) @Html.PagedListPager((IPagedList)ViewBag.posts, page => Url.Action("Index", new { page = page }), PagedListRenderOptions.OnlyShowFivePagesAtATime) @Html.PagedListPager((IPagedList)ViewBag.posts, page => Url.Action("Index", new { page = page }), new PagedListRenderOptions { LinkToFirstPageFormat = "<< ابتدا", LinkToPreviousPageFormat = "< قبلی", LinkToNextPageFormat = "بعدی>", LinkToLastPageFormat = "آخرین >>" }) The type or namespace name 'PagedListRenderOptions' could not be found (are you missing a using directive or an assembly reference?)