Copied to clipboard

Flag this post as spam?

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


  • Lee Fear 7 posts 97 karma points
    Sep 23, 2019 @ 11:50
    Lee Fear
    0

    How do I grab a property from the homepage for child pages

    I have a master page and I want to display the footerText property of the homepage in the footer of the masterpage. This works fine for the homepage but child pages display nothing as I imagine the property does not appear on these and so the masterpage can not get hold of it.

    How do I get hold of the footerText property in the homepage and display it correctly in the masterpage so that is shows on all child pages?

  • Jonathan Distenfeld 105 posts 618 karma points
    Sep 23, 2019 @ 12:17
    Jonathan Distenfeld
    0

    Hi Lee,

    this should give you an idea of how to achieve this:

    @Model.AncestorOrSelf<HomePage>().FooterText
    

    Assuming your homepage has doctype "HomePage" and current content is a child from your homepage. If you have problems using this, please provide some information about your structure.

    I hope this helps,

    ~ Jonathan

  • Lee Fear 7 posts 97 karma points
    Sep 23, 2019 @ 12:31
    Lee Fear
    0

    I have just tried that code as follows

    <div>Footer Stuff @Model.AncestorOrSelf<HomePage>().FooterText</div>
    

    I get the following error

    Compiler Error Message: CS1503: Argument 1: cannot convert from 'method group' to 'HelperResult'

    The home page is the root page and the page I am trying to display on is a direct child of the homepage. They are the only 2 pages in the site at the moment.

  • Jonathan Distenfeld 105 posts 618 karma points
    Sep 23, 2019 @ 12:49
    Jonathan Distenfeld
    100

    Hi Lee,

    in Razor you are not able to use generic types directly like this. Try following:

    @{
        string footerText = Model.AncestorOrSelf<HomePage>().FooterText; 
    }
    <div>Footer Stuff @footerText</div>
    

    ~ Jonathan

  • Lee Fear 7 posts 97 karma points
    Sep 23, 2019 @ 12:57
    Lee Fear
    0

    Awesome thanks. That works a treat!

Please Sign in or register to post replies

Write your reply to:

Draft