Copied to clipboard

Flag this post as spam?

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


  • Jan Egil Kristiansen 37 posts 160 karma points
    Jun 12, 2015 @ 08:59
    Jan Egil Kristiansen
    0

    How do I get the Page Title of a child document?

    I have the following code:

        @foreach(var p in feed.Children)
    {
        <div class="feedItem">
            @{
                var itemName = p.Name;
                // var itemName = p.Title;
                // var itemName = p.PageTitle;
            }
            @itemName
        </div>
    
    }
    

    That works fine in getting the name of the child document. But how do I get the Page title of the child?

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jun 12, 2015 @ 09:29
    Jamie Pollock
    0

    Hi Jan,
    I assume you're using the Umbraco DynamicNode objects.

    In which case you'd use the property alias this isn't turned into pascal case it maintains the style which the Umbraco property editor creates which is usually camel case. (eg pageTitle not PageTitle)

    @foreach(var p in feed.Children)
    {
        <div class="feedItem">
            @{
                        var itemName = p.pageTitle;
            }
            @itemName
        </div>    
    }
    

    Thanks,
    Jamie

  • Jan Egil Kristiansen 37 posts 160 karma points
    Jun 12, 2015 @ 10:07
    Jan Egil Kristiansen
    0

    Thanks.

    But the compilation still fails. And I should of course have included the message:

    Compiler Error Message: CS1061: 'Umbraco.Core.Models.IPublishedContent' does not contain a definition for 'pageTitle' and no extension method 'pageTitle' accepting a first argument of type 'Umbraco.Core.Models.IPublishedContent' could be found (are you missing a using directive or an assembly reference?)

    Jan Egil

  • Kevin Jump 2343 posts 14890 karma points MVP 8x c-trib
    Jun 12, 2015 @ 10:11
    Kevin Jump
    0

    Hi you are probably using Typed Content. (That is what comes from Model.Content )

    which means you need to call

    p.GetPropertyValue<string>("pageTitle", p.Name) 
    

    which will get the pageTitle property and if it's not set return a default of the Node Name.

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jun 12, 2015 @ 10:11
    Jamie Pollock
    0

    Ohhh it's a IPublishedContent in which case do the following.

    @foreach(var p in feed.Children)
    {
        <div class="feedItem">
            @{
                        var itemName = p.GetPropertyValue<string>("pageTitle");
            }
            @itemName
        </div>    
    }
    

    The generic type of string is up to you, seeing as you're doing with in Razor you shouldn't need it as the @ will just call ToString() on render anyway.

  • Jan Egil Kristiansen 37 posts 160 karma points
    Jun 12, 2015 @ 11:09
    Jan Egil Kristiansen
    0

    Thanks. That works.

  • Jamie Pollock 174 posts 853 karma points c-trib
    Jun 12, 2015 @ 11:11
    Jamie Pollock
    0

    Awesome fella, make sure you mark the right answer post as the solution as read as this helps people who view these topics later on.

    Karma makes the world go round after all. :)

  • sla 2 posts 22 karma points
    Jun 12, 2015 @ 15:17
    sla
    0
    @foreach(var p in feed.Children)
    {
        <div class="feedItem">
           @p.pageTitle;
        </div>    
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft