2

is there any simple way for implementing paging in presentation layer in asp.net boilerplate solution like PagedList in PagedList.Mvc Nuget package.

I installed PagedList.Mvc package from Nuget gallery and change my Index method to this :

 public async Task<ActionResult> Index(int? page)
        {
            var pageIndex = (page ?? 1) - 1; //NewsService expects a 0 for the first page
            var pageSize = 3;
            int totalUserCount; // will be set by call to GetAllNews due to _out_ paramter :-|
            var newsResult = await _newsAppService.GetAllNews(new PagedResultRequestDto { MaxResultCount = pageSize });
            totalUserCount = newsResult.TotalCount;

            var news = newsResult.Items;
            var newsAsIPagedList = new StaticPagedList<NewsDto>(news, pageIndex + 1, pageSize, totalUserCount);

            ViewBag.OnePageOfNews = newsAsIPagedList;


            var model = new NewsListViewModel
            {
                News = news
            };

            return View(model);
        }

and in View I add this under the news table :

@Html.PagedListPager((IPagedList)ViewBag.OnePageOfNews, page => Url.Action("Index", new { page }))

but when i click on number for example number 2 or 3 the table refresh but change data.

2
  • is it .net core application ? Dec 21, 2017 at 9:19
  • @Saneeshkunjunni no it's asp.net mvc application.
    – M.Sabzi
    Dec 21, 2017 at 13:37

2 Answers 2

3

ASP.NET Boilerplate doesn't provide this, but it's simple to use PagedList.Mvc as you suggested.

It's easy to modify UsersController.Index to use manual paging:

public async Task<ActionResult> Index(int? page)
{
    var pageIndex = (page ?? 1) - 1;
    var pageSize = 10;
    int totalUserCount;

    var skipCount = pageIndex * pageSize;
    var pagedResult = await _userAppService.GetAll(new PagedResultRequestDto { MaxResultCount = pageSize, SkipCount = skipCount });
    totalUserCount = pagedResult.TotalCount;

    var users = pagedResult.Items;
    var usersAsIPagedList = new StaticPagedList<UserDto>(users, pageIndex + 1, pageSize, totalUserCount);

    ViewBag.OnePageOfUsers = usersAsIPagedList;

    // Code below unchanged
    var roles = (await _userAppService.GetRoles()).Items;
    var model = new UserListViewModel
    {
        Users = users,
        Roles = roles
    };
    return View(model);
}
0
2

Yes, you can in boilerplate application from presentation view if it is small then you can go with bootstrap datatable if it is not then you can go with manual

For pagination you can use manual classes and list extension function

public static class ListExtensions
{
 public static PaginatedList<T> ToPagedList<T>(this IQueryable<T> list, int page, int pageSize)
{
    int count = list.Count();
    var items = list.Skip((page - 1) * pageSize).Take(pageSize).ToList();
    return new PaginatedList<T>(items, count, page, pageSize);
}
public static async Task<PaginatedList<T>> ToAsycPagedList<T>(this IQueryable<T> list, int page, int pageSize)
{
    return await PaginatedList<T>.CreateAsync(list, page, pageSize);
}
}

and pagination class like this

public class PaginatedList<T> : List<T>// where T: IQueryable<T>, IEnumerable<T>, ICollection<T>
{
public int PageIndex { get; private set; }
public int TotalPages { get; private set; }
public int RowCount { get; set; }
public int PageSize { get; set; }
public int FirstRowOnPage
{

    get { return (PageIndex - 1) * PageSize + 1; }
}
public int FirstPage
{

    get { return 1; }
}

public int LastRowOnPage
{
    get { return Math.Min(PageIndex * PageSize, RowCount); }
}
public int LastPage
{
    get { return (int)Math.Floor((decimal)RowCount / PageSize); }
}

public PaginatedList(List<T> items, int count, int pageIndex, int pageSize)
{
    PageIndex = pageIndex;
    RowCount = count;
    PageSize = pageSize;
    TotalPages = (int)Math.Ceiling(count / (double)pageSize);
    this.AddRange(items);
}

public bool HasPreviousPage
{
    get
    {
        return (PageIndex > 1);
    }
}

public bool HasNextPage
{
    get
    {
        return (PageIndex < TotalPages);
    }
}

public static async Task<PaginatedList<T>> CreateAsync(IQueryable<T> source, int pageIndex, int pageSize)
{
    var count = await source.CountAsync();
    var items = await source.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToListAsync();
    return new PaginatedList<T>(items, count, pageIndex, pageSize);
}
}

after adding those two classes you can use this as

var data = await list.OrderBy(x => x.fieldname).ToAsycPagedList(page, pageSize);
var data = list.OrderBy(x => x.fieldname).ToPagedList(page, pageSize);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.