Skip to content

Instantly share code, notes, and snippets.

@nul800sebastiaan
Last active December 24, 2018 09:53
Show Gist options
  • Save nul800sebastiaan/8641249 to your computer and use it in GitHub Desktop.
Save nul800sebastiaan/8641249 to your computer and use it in GitHub Desktop.
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(IPublishedContent content) : base(content)
{ }
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(RenderModel model)
{
var blogOverviewModel = new BlogOverview(model.Content);
blogOverviewModel.BlogPosts = GetPagedBlogPosts(blogOverviewModel);
return CurrentTemplate(blogOverviewModel);
}
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>
@bjarnef
Copy link

bjarnef commented Nov 17, 2017

@nul800sebastiaan something is missing here.
It is always showing page 1 even you browse to page 2, 3, etc. and next page link was always "?page=2", because it doesn't request the querystring.

I have a similar case with articles and have modified the following:

Removed static from method to be able to request querystring inside this method.

private IEnumerable<IPublishedContent> GetPagedPagedArticles(PageThemeCategoryViewModel model)
{
    ...
}

Added a few lines before setting init page.

var page = Request.QueryString["page"];
if(!string.IsNullOrEmpty(page))
{
    var convert = page.TryConvertTo<int>();
    if (convert.Success)
        model.Page = convert.Result;
}

if (model.Page == default(int))
    model.Page = 1;

I also added the following when collection is empy, so in view it says Page 1 of 1 instead of Page 1 of 0.

int total = articles.Count();
model.TotalPages = total > 0 ? Convert.ToInt32(Math.Ceiling((double)total / pageSize)) : 1;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment