and I'm rendering the partial like this @Html.Partial("ListBlogItems") both on the index and the blogs page, however, I want to show three blog posts on the index and show all records on the blog page.
So is there a way to limit the partial on the index to only render three records?
From your selection query, what you've got is a List in C# so you can use LINQ to filter the output.
So selection.Take(3) will achieve what you are after, but then you won't be able to re-use the partial. What you can do is pass the collection of items to the partial and then iterate through those as you have done.
Example for that: @Html.Partial("ListBlogItems", blogItems) where you instantiate blogItems beforehand. That will mean your model in the partial is now the blog items you passed in and you'll need to update the @inherits statement to match.
e.g. @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<IEnumerable<IPublishedContent>>
The other thing to be aware of is that if you are already on the blog listing page then @Model.Content.Children("blogPost") will give you the blog pages anyhow and so is a more performant query as you're not traversing the content tree as much.
Only show three records on partial
Hi,
So I have a blog partial
and I'm rendering the partial like this
@Html.Partial("ListBlogItems")
both on the index and the blogs page, however, I want to show three blog posts on the index and show all records on the blog page.So is there a way to limit the partial on the index to only render three records?
Hey Ben,
From your selection query, what you've got is a List in C# so you can use LINQ to filter the output.
So
selection.Take(3)
will achieve what you are after, but then you won't be able to re-use the partial. What you can do is pass the collection of items to the partial and then iterate through those as you have done.Example for that:
@Html.Partial("ListBlogItems", blogItems)
where you instantiateblogItems
beforehand. That will mean your model in the partial is now the blog items you passed in and you'll need to update the @inherits statement to match.e.g.
@inherits Umbraco.Web.Mvc.UmbracoTemplatePage<IEnumerable<IPublishedContent>>
The other thing to be aware of is that if you are already on the blog listing page then
@Model.Content.Children("blogPost")
will give you the blog pages anyhow and so is a more performant query as you're not traversing the content tree as much.is working on a reply...