getting page properties in partial for nested content
In a page template I refer to a bunch of partials for nested content like this where "block" is the nested content element.
else if (block.ContentType.Alias == "childPages") {@Html.Partial("DRC/modules/childpages", block)}
In most partials this works just fine. However, a single partial causes errors. A simplified version of the partial looks like this
@inherits Umbraco.Web.Mvc.UmbracoViewPage<IPublishedElement>
@using Umbraco.Web.Models;
<section class="childpages">
@if (Model.HasValue("heading")) {<h3>@Model.Value("heading")</h3>}
@{
// Get the current page
var currentPage = Model.Ancestor();
if (currentPage != null)
{
// Get the children of the current page
var childPages = currentPage.Children().Where(x => x.IsVisible()).OrderByDescending(x => x.CreateDate);
if (childPages.Any())
{
// Do something for each child page
foreach (var item in childPages)
{
<p>do something</p>
}
}
}
}
</section>
Since I am pushing "block" from the page template to the partials, Model seems to be the way to access properties from the nested element. Problem then seems to be to access the page that holds the nested element, since I cannot refer to this as Model.
Error message: 'IPublishedElement' does not contain a definition for 'Ancestor' and the best extension method overload 'PublishedContentExtensions.Ancestor(IPublishedContent)' requires a receiver of type 'IPublishedContent'
at System.Web.Compilation.AssemblyBuilder.Compile()
I tried changing IPublishedElement to IPublishedContent in line 1, but this just gives me another error message: Cannot bind source type Umbraco.Web.PublishedModels.ChildPages to model type Umbraco.Core.Models.PublishedContent.IPublishedContent.
Yes, in this context, inside your partial view, 'Model' represents the block that you sent in which is of IPublishedElement type, and an IPublishedElement doesn't have the notion of where it sits in the Umbraco Content Tree, and therefore doesn't have Ancestors or Children.
However, Inside any view that inherits UmbracoViewPage you should be able to get access to the IPublishedContent of the current page via
var currentPage = Umbraco.AssignedContentItem;
which would then give you access to the page the block is on to query for ancestors or children.
Alternatively, you could create your own custom model something like
public class MySuperModel {
public IPublishedElement CurrentBlock {get;set;}
public IPublishedContent CurrentPage {get;set;{
}
and then use that to pass to your partial view
else if (block.ContentType.Alias == "childPages") {@Html.Partial("DRC/modules/childpages", new MySuperModel(){CurrentBlock = block, CurrentPage = Model)};
it depends a lot on the structure of your site and how you prefer to write code etc,. I'll often build a site as simply as possible and then refactor it to use more complete viewmodels when I spot patterns...
Indeed, the Umbraco.AssignedContentItem thing did the trick. I was not aware of that one, but it seems like something that could solve a lot of issues down the line. Thanks very much.
Not being a "proper" coder I feel most comfortable just working in the V-part of MVC, but at some point I surely will look into the custom model which seems potentially a very powerful tool.
getting page properties in partial for nested content
In a page template I refer to a bunch of partials for nested content like this where "block" is the nested content element.
In most partials this works just fine. However, a single partial causes errors. A simplified version of the partial looks like this
Since I am pushing "block" from the page template to the partials, Model seems to be the way to access properties from the nested element. Problem then seems to be to access the page that holds the nested element, since I cannot refer to this as Model.
Error message: 'IPublishedElement' does not contain a definition for 'Ancestor' and the best extension method overload 'PublishedContentExtensions.Ancestor(IPublishedContent)' requires a receiver of type 'IPublishedContent' at System.Web.Compilation.AssemblyBuilder.Compile()
I tried changing IPublishedElement to IPublishedContent in line 1, but this just gives me another error message: Cannot bind source type Umbraco.Web.PublishedModels.ChildPages to model type Umbraco.Core.Models.PublishedContent.IPublishedContent.
Any suggestions?
Hi Jesper
Yes, in this context, inside your partial view, 'Model' represents the block that you sent in which is of IPublishedElement type, and an IPublishedElement doesn't have the notion of where it sits in the Umbraco Content Tree, and therefore doesn't have Ancestors or Children.
However, Inside any view that inherits UmbracoViewPage you should be able to get access to the IPublishedContent of the current page via
which would then give you access to the page the block is on to query for ancestors or children.
Alternatively, you could create your own custom model something like
and then use that to pass to your partial view
and change your inherits statement to be:
it depends a lot on the structure of your site and how you prefer to write code etc,. I'll often build a site as simply as possible and then refactor it to use more complete viewmodels when I spot patterns...
regards
Marc
Hi Marc
Indeed, the Umbraco.AssignedContentItem thing did the trick. I was not aware of that one, but it seems like something that could solve a lot of issues down the line. Thanks very much.
Not being a "proper" coder I feel most comfortable just working in the V-part of MVC, but at some point I surely will look into the custom model which seems potentially a very powerful tool.
Have a wonderful day.
is working on a reply...