Kacey Ezerioha
Kacey Ezerioha

Reputation: 1198

Can't get Properties of a Specific Child Page

I just started using umbraco 4 days ago and I'm stuck with this problem.

I have a page named "About Us" that is a child under "Home" and I need to get some of its properties in the "Home" Page.

I created a partial view macro to do this but I get the error loading partial view script instead.

Below is my code:

@{ var selection = CurrentPage.Children.Where(x => x.Name == "aboutus"); }

@if (selection.Any())
{
    <ul>
        @foreach (var item in selection)
        {
            <div class="sc-inner">
                <h4>
                   @item.PageTitle</h4>
                <p>
                   @Umbraco.Truncate(@item.mainAboutDescription, 100)</p>
                <a class="btn btn-danger" href="@item.Url">Read More...</a>
            </div>
            break;
        }
    </ul>
}

Can someone please tell me what I'm doing wrong?

Thanks in advance.

Edits: The error on the website I got is below;

Error loading Partial View script (file: ~/Views/MacroPartials/homePage_aboutUsSection.cshtml)

Upvotes: 0

Views: 638

Answers (2)

wingyip
wingyip

Reputation: 3536

Very bad practice to use the Name property for querying in your code - what if somebody changes the Name to About for example. Your code would break. If there is something else that is unique on the node then it would be better.

For example if you have an aboutUs document type for your about us page you could use:

Model.Content.Children.Where(x => x.DocumentTypeAlias == "exactNameOfYourDocTypeAliasHere")

or, although not so robust, the Id of the page could be used.

Model.Content.Children.Where(x => x.Id == 1234) 

The Id is far less likely to change than the Name of a key page like this.

I would generally not use the Id in code either but it is far better than Name

Upvotes: 2

Anth12
Anth12

Reputation: 1897

Assuming the partial is inheriting UmbracoTemplatePage the CurrentPage property is a dynamic, you cannot use lambda expressions as an argument to a dynamically dispatched operation.

If you wish to use Linq to query the content use Model.Content rather than CurrentPage which is an IPublishedContent e.g.

@{ var selection = Model.Content.Children.Where(x => x.Name == "aboutus"); }

Note: the Name property will return the documents Name as entered in the CMS most likely 'About Us' instead of 'aboutus'

Using the about will return an IEnumerable<IPublishedContent> so you will need to use the GetPropertyValue method rather than accessing the content dynamically:

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage

@{ var selection = Model.Content.Children.Where(x => x.Name == "aboutus"); }

@if (selection.Any())
{
    <ul>
        @foreach (var item in selection)
        {
            <li class="sc-inner">
                <h4>
                    @item.GetPropertyValue("pageTitle")
                </h4>
                <p>
                    @Umbraco.Truncate(@item.GetPropertyValue<string>("mainAboutDescription"), 100)
                </p>
                <a class="btn btn-danger" href="@item.Url">Read More...</a>
            </li>
            break;
        }
    </ul>
}

Upvotes: 2

Related Questions