Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jesper Skjønnemand 65 posts 440 karma points c-trib
    Sep 24, 2023 @ 10:22
    Jesper Skjønnemand
    0

    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.

    Any suggestions?

  • Marc Goodson 2141 posts 14344 karma points MVP 8x c-trib
    Sep 24, 2023 @ 14:13
    Marc Goodson
    100

    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

    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)};
    

    and change your inherits statement to be:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<MyCustomModel>
    

    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

  • Jesper Skjønnemand 65 posts 440 karma points c-trib
    Sep 24, 2023 @ 16:40
    Jesper Skjønnemand
    0

    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.

Please Sign in or register to post replies

Write your reply to:

Draft