Search code examples
umbracoumbraco7

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)


Solution

  • 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>
    }