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>
             }
  
               
       
        @if (totalPages > 1)
        {
            <div class="pagination">
                <ul>
                    @if (page > 1)
                    {
                        <li><a href="?page=@(page-1)">Prev</a></li>
                    }
                    @for (int p = 1; p < totalPages + 1; p++)
                    {
                        <li class="@(p == page ? "active" : string.Empty)">
                            <a href="?page=@p">@p</a>
                        </li>
                    }
                    @if (page < totalPages)
                    {
                        <li><a href="?page=@(page+1)">Next</a></li>
                    }
                </ul>
            </div>
        }
       
   

Comments

Popular posts from this blog

Umbraco - Reset the Admin Password via SQL Server

Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco

Umbraco Macro for IFrame when Embedding Video, Vimeo Universal Player, or other types of IFrame on to a Page XSLT version and Razor Version