Question

I have a news list under which there are a load of News Items. I'm trying to get the page name of the news list to display on each news item but this code isn't cutting it. I get an error saying "Umbraco.Web.Models.RenderModel' does not contain a definition for 'AncestorOrSelf'"

I want this to use levels rather than nodeID so it's reuseble on other pages. This is what I've got so far:-

   @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@{
    Layout = "BasePage.cshtml";

    var sectionTitle = Model.AncestorOrSelf(2).pageName;
}
            <div id="contentHeader">
                <div class="row contentHeader">
                    <div class="col-md-6 page-title no-left-pad">
                        <h1>@sectionTitle</h1>
                    </div>
                    <div class="col-md-6 no-right-pad">
                        <a href="/care-homes" class="btn btn-care-profile pull-right">Use our CareFinder</a>
                    </div>
                </div>
            </div>

            @RenderBody()

Any advice appreciated as I can't find any reason for the error anywhere.

Thanks

Was it helpful?

Solution

I think what you should be looking for is:

Model.Content.AncestorOrSelf(2).Name

Model returns a RenderModel object but what you want is the IPublishedContent object which you will find in the Model.Content property.

You should of course perform a null check before attempting to access the name e.g.

if(Model.Content.AncestorOrSelf(2) != null)
{
    sectionTitle = Model.Content.AncestorOrSelf(2).Name;
}

OTHER TIPS

Try using Model.Content.AncestorOrSelf or Model.Content.AncestorsOrSelf The available input variables are int (level) and string (NodeTypeAlias)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top