Last active
December 24, 2018 09:53
-
-
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/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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">← 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 →</a> | |
} | |
</nav> | |
</section> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@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.Added a few lines before setting init page.
I also added the following when collection is empy, so in view it says
Page 1 of 1
instead ofPage 1 of 0
.