Computer Magic Logo
Display the data with paging

Sunday, August 23, 2015

Published by Aristotelis Pitaridis

Deleting data is easy because we just supply the ID of the record we want to delete so let's see how to display the data with paging. We are not going to implement the paging functionality. We will just display the information that PetaPoco gives us so that we will see how to use the paging. We type the following code in a new view.

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using ComputerMagic.PetaPoco.Models
@using ComputerMagic.PetaPoco.Repository
@{
    Layout = null;
	
    var PagedAuthors = Authors.GetAllPaged(1, 10);
}

<ul>
    @foreach (var item in PagedAuthors.Items)
    {
    <li>@item.Name @item.Surname</li>
    }
</ul>
Total Pages : @PagedAuthors.TotalPages<br />
Current Page : @PagedAuthors.CurrentPage<br />
Total records : @PagedAuthors.TotalItems<br />
Records per page : @PagedAuthors.ItemsPerPage<br />

As we can see in the code block we use the GetAllPaged member function of the authors repository and we ask the first page and we want to have 10 records in each page. Our table has only two records so we will get only two records.

The Items property of the PagedAuthors object returned by the repository contains the authors that we asked. If we had more than 10 authors the list would contain the first 10 records because we asked the first page and we asked to have 10 records per page.

After the foreach loop we have four values which are useful in order to implement the paging of a list. These values are the number of total pages, the number of the current page, the total number of records and the number of records per page.

The generated HTML is the following:

<ul>
    <li>Douglas Adams</li>
    <li>Jules Verne</li>
</ul>
Total Pages : 1<br />
Current Page : 1<br />
Total records : 2<br />
Records per page : 10<br />

The optical output is the following: