Hi,
I've created a controller which is used to display a title of the blog on the homepage. Just now I do this like so :
List<BlogPostListModel> model = new List<BlogPostListModel>();
IPublishedContent blog = CurrentPage.AncestorOrSelf(1).DescendantsOrSelf().Where(x => x.DocumentTypeAlias == "blog").FirstOrDefault();
foreach (IPublishedContent blogpost in blog.Children)
{
model.Add(new BlogPostListModel(blogpost.Name));
}
return PartialView("~/path/to/partial", model);
This is fine except, it displays all the blog titles, what I want to do is check to see if the number of blogs within the CMS is more than the number wanted from the backoffice. In the backoffice I have a slider which allows the user to pick how many blogs to show on the homepage. So if I have 10 blogs, I only want to show the 3 latest.
I was thinking something like
var numToShow = blog.GetPropertyValue<int>("howManyPostsShouldBeShown");
if (blog.Children.Count() > numToShow)
{
}
would work but my issue now is, how to I only show the number of blogs I want within the If statement?
Hope that makes sense, been scratching my head all evening.
Thanks for the share. I rely heavily on the ContentService, so this might be a field where I can improve. But I'd say it depends on how many published items you've got. If OP only got a few houndred items, Children/Descendants is fine.
Showing X number of posts via Controller
Hi, I've created a controller which is used to display a title of the blog on the homepage. Just now I do this like so :
This is fine except, it displays all the blog titles, what I want to do is check to see if the number of blogs within the CMS is more than the number wanted from the backoffice. In the backoffice I have a slider which allows the user to pick how many blogs to show on the homepage. So if I have 10 blogs, I only want to show the 3 latest.
I was thinking something like
would work but my issue now is, how to I only show the number of blogs I want within the If statement?
Hope that makes sense, been scratching my head all evening.
O.
Hey Owain,
You can use Linq to do this really quickly.
(You might to do Children.Skip(0).Take(numberToShow) I'm not 100% on that though.)
That will take upto the number you've selected to show.
So, if blog.Children.Count() = 5 and numToShow = 6, it will only take 5. And if numToShow = 4 it will only take 4.
Cheers,
Nik
Oh boy, I was close :)
Just didn't have the .Take.
Thanks everyone!!
Yeah, take a look at Take in Linq like Nik said.
Although I would say you may want to use examine / xpath for the query as Descendants calls are slower.
Have a look at these great slides from dawoe (I can't remember his actual name :'( )
https://www.slideshare.net/dawoe/umbraco-duugfest-17-the-need-for-speed
Thanks for the share. I rely heavily on the ContentService, so this might be a field where I can improve. But I'd say it depends on how many published items you've got. If OP only got a few houndred items, Children/Descendants is fine.
The content service should not be used for front end requests, it makes database calls instead of calls to the Umbraco cache.
The code above uses the cache as well.
is working on a reply...