I'm trying to setup paging for my blog and came across a thread which had some code to add.
I've added it and made some tweaks but I cant seem to get it to work, wondering if someone could have a quick look and let me know if I'm missing something.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Blog>
@using ContentModels = Umbraco.Web.PublishedContentModels;
@{
Layout = "Master.cshtml";
int pageSize = 2; // How many items per page
int page; // The page we are viewing
/* Set up parameters */
if (!int.TryParse(Request.QueryString["page"], out page))
{
page = 1;
}
/* This is your basic query to select the nodes you want */
var nodes = Model.Content.Children.Where(x => x.DocumentTypeAlias == "blogItem").OrderBy(x=>x.CreateDate);
int totalNodes = nodes.Count();
int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
/* Bounds checking */
if (page > totalPages)
{
page = totalPages;
}
else if (page < 1)
{
page = 1;
}
}
}
I'm still having trouble trying to get paging to work, would appreciate if somebody could point me in the right direction.
This is my code;
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.Blog>
@using ContentModels = Umbraco.Web.PublishedContentModels;
@{
Layout = "Master.cshtml";
int pageSize = 3; // How many items per page
int page; // The page we are viewing
/* Set up parameters */
if (!int.TryParse(Request.QueryString["page"], out page))
{
page = 1;
}
/* This is your basic query to select the nodes you want */
var nodes = Model.Content.Children.Where(x => x.DocumentTypeAlias == "blogItem").OrderBy("CreateDate desc");
int totalNodes = nodes.Count();
int totalPages = (int)Math.Ceiling((double)totalNodes / (double)pageSize);
/* Bounds checking */
if (page > totalPages)
{
page = totalPages;
}
else if (page < 1)
{
page = 1;
}
}
<div class="body-section">
<div class="body-container w-container">
<h1 class="main-page-heading">@Umbraco.Field("blogTitle")</h1>
@Html.Partial("ListBlogItems")
@{
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>
}
}
</div>
</div>
I think I know what the issue is I just don't know how to resolve it, I assume its because I'm using partial view and the top part of the code should be in the partial view section. But when doing that it doesn't read the "totalpages" at the bottom.
add paging to my blog
Hello all,
I'm trying to setup paging for my blog and came across a thread which had some code to add.
I've added it and made some tweaks but I cant seem to get it to work, wondering if someone could have a quick look and let me know if I'm missing something.
Hi,
All looks in order you just need to filter your nodes when outputting them:
Something like below will work:
Thanks, Lewis
Hello,
I'm still having trouble trying to get paging to work, would appreciate if somebody could point me in the right direction.
This is my code;
I think I know what the issue is I just don't know how to resolve it, I assume its because I'm using partial view and the top part of the code should be in the partial view section. But when doing that it doesn't read the "totalpages" at the bottom.
Hi Matt, can you post the code inside you partial view. It seems that you don't pass any parameters.
Mila
Yea I guessed that was the issue, but when I enter anything in the partial view it seems my main page wont pick up any of the code.
Hi Matt, I don't see where you iterate through your nodes. Bellow is an example without partial view.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
Hello Mila,
Thanks, so in theory I can scrap the idea of using the partial view? and just use the query to select and dispay my nodes?
I tried your code just on my blog main page and get the following error;
Hi Matt, As the screenshot says PageSize should be pageSize inside .Take(pageSize).
is working on a reply...