Skip to main content

Posts

Showing posts from April, 2020

Umbraco 8 Pagination

Umbraco 8 Pagination / Paging:

@inherits Umbraco.Web.Mvc.UmbracoViewPage

@{
    Layout = "master.cshtml";

    var pageSize = 8;

    if(Model.HasValue("numberOfItemsPerPage")){
    pageSize = Model.Value<int>("numberOfItemsPerPage");}

    var page = 1; int.TryParse(Request.QueryString["page"], out page);

    @* This line assumes you have a property called lastUpdated*@
    var items = Model.Children().OrderByDescending(x => x.Value("lastUpdated"));
    @* Remove the line above and use this line to keep it simple *@
    @* var items = Model.Children()); *@


    var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);

    if (page > totalPages)
    {
        page = totalPages;
    }
    else if (page < 1)
    {
        page = 1;
    }

}

@foreach (var item in items.Skip((page - 1) * pageSize).Take(pageSize))
            {
           <a href="@item.Url">@item.Name</a>
             }