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.
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.
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.
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 :)
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
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
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.
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
Since I posted this I have learned to use Umbraco.Content ((CurrentPage.AncestorsOrSelf (1).First ()).Id)
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!
:)
is working on a reply...