tobbbe
1/19/2016 - 12:01 PM

What's this Umbraco route hijacking all about? (more info in this blog post: http://cultiv.nl/blog/whats-this-umbraco-route-hijacking-all-ab

What's this Umbraco route hijacking all about? (more info in this blog post: http://cultiv.nl/blog/whats-this-umbraco-route-hijacking-all-about/)

// MODEL
using System.Collections.Generic;
using System.Linq;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;

namespace Cultiv.Models
{
    public class BlogOverview : RenderModel
    {
        public BlogOverview() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
        
        public int Page { get; set; }

        public int TotalPages { get; set; }

        public int PreviousPage { get; set; }

        public int NextPage { get; set; }

        public bool IsFirstPage { get; set; }

        public bool IsLastPage { get; set; }

        public IEnumerable<IPublishedContent> BlogPosts { get; set; }
    }
}


//CONTROLLER
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Cultiv.Models;
using Umbraco.Core.Models;
using Umbraco.Web.Mvc;

namespace Cultiv.Controllers
{
    public class BlogOverviewController : RenderMvcController
    {
        public ActionResult BlogOverview(BlogOverview model)
        {
            model.BlogPosts = GetPagedBlogPosts(model);

            return CurrentTemplate(model);
        }

        private static IEnumerable<IPublishedContent> GetPagedBlogPosts(BlogOverview model)
        {
            if (model.Page == default(int))
                model.Page = 1;

            const int pageSize = 5;
            var skipItems = (pageSize*model.Page) - pageSize;

            var posts = model.Content.Children.ToList();
            model.TotalPages = Convert.ToInt32(Math.Ceiling((double) posts.Count()/pageSize));

            model.PreviousPage = model.Page - 1;
            model.NextPage = model.Page + 1;

            model.IsFirstPage = model.Page <= 1;
            model.IsLastPage = model.Page >= model.TotalPages;

            return posts.OrderByDescending(x => x.CreateDate).Skip(skipItems).Take(pageSize);
        }
    }
}

//VIEW
@using Cultiv.Models
@inherits UmbracoViewPage<BlogOverview>
@{
    Layout = "~/Views/Site.cshtml";
}

@foreach (var post in Model.BlogPosts)
{
    <h2 class="post-title" itemprop="name"><a itemprop="url" href="@post.Url">@post.Name</a></h2>
    <p itemprop="articleBody">@Cultiv.StringHelpers.GetSummary(post.GetPropertyValue<string>("bodyText"), 50)...</p>
}

<section class="pagination-wrapper">
    <nav class="pagination" role="pagination">

        @if (Model.IsFirstPage == false)
        {
            <a class="newer-posts" href="?page=@Model.PreviousPage">&larr; Newer Posts</a>
        }

        <span class="page-number">Page <span class="number">@Model.Page</span> of <span class="number">@Model.TotalPages</span></span>

        @if (Model.IsLastPage == false)
        {
            <a class="older-posts" href="?page=@Model.NextPage">Older Posts &rarr;</a>
        }

    </nav>
</section>