Copied to clipboard

Flag this post as spam?

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


  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Jul 04, 2014 @ 16:35
    Paul Seal
    0

    How to reference home page from inside a view model

    Hi

    I have been able to get the home page object when using normal views or partia views, but now I am using a view which is tied to a view model I can't find how I can reference the home page. I want to use some content from it.

    Please could you help me?

    Thanks

    Paul

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jul 04, 2014 @ 16:50
    Jan Skovgaard
    1

    Hi Paul and welcome to our :)

    Have you tried searching in the documentation? (Unfortunately it can be confusing to figure out what to use and what is legacy etc. - But there is some good stuff in there though).

    I think what you need is this page from the documentation http://our.umbraco.org/documentation/Reference/Mvc/querying - There is some examples of how to retrieve nodes based on an id and you can probably also use the traversion methods as well.

    Hope this helps.

    /Jan

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Jul 04, 2014 @ 17:11
    Paul Seal
    0

    Thanks Jan

    I was able to reference the home page using its id.

    var homePage = @Umbraco.Content(1064);

    I found the id by looking in the contentXml table in the database. It was the first record in there.

    I don't normally like using "magic numbers" in code, but because I know the home page is never going to change, I'm happy to do it in this instance.
    I suppose if I really didn't want to use a hard coded number I could get the id of the home page in the controller and pass it into the model.

    Thanks for your help.

  • Charles Afford 1163 posts 1709 karma points
    Jul 08, 2014 @ 13:56
    Charles Afford
    0

    Hi Paul,

    Dont use an ID, i will end in tears.  Content gets deleted changed and you cannot assert that the IDS are going to be the same across enviroments.  What version of umbraco are you using and what is the document type on the home node?

    You could just write a helper methof that takes in a (Node) or (IPublishedContent)  and using recursion walks up the tree until it finds the node of the 'Home' doc type.  very easy :)

    Charlie

  • Paul Seal 524 posts 2889 karma points MVP 6x c-trib
    Jan 22, 2015 @ 21:50
    Paul Seal
    0

    Since I posted this I have learned to use Umbraco.Content ((CurrentPage.AncestorsOrSelf (1).First ()).Id)

  • Tim 1193 posts 2675 karma points MVP 3x c-trib
    Feb 03, 2015 @ 15:34
    Tim
    0

    Hi Paul,

    You should just be able to use CurrentPage.AncestorsOrSelf (1).First ()

    As this is already a Content item, no need to create another one using it's ID!

    :)

Please Sign in or register to post replies

Write your reply to:

Draft