I'm new to using Umbraco (I started a few days before V8 was launched) and I am struggling to work out the Razor syntax for selecting a specific node in my page structure when I'm on the master template.
My page structure looks like this:
I would like to select the Case Studies page from the master template so that I can list out the children of the case studies page.
This is what I have done on my master template (it works but I don't like it):
var navItems = Model.Root().Children().Where(x => x.IsVisible());
var homePage = Model.Root();
var caseStudiesNode = Model.Root();
foreach (var item in navItems)
{
if (item.Id == 1093)
{
caseStudiesNode = item;
}
}
This feels like a hacky way to do it and I'm sure theres a better way, can anyone help?
If it was me i would create a specific document type for my case studies and find the first descendant of my root page of that document type.
Alternatively you could create a property on the document type you have made for your frontpage with a Content Picker. Then in razor use the GetPropertyValue
Thanks for the advice, I tried accessing the first descendant of that document type but it wouldn't let me run .Children() on that variable that I stored it in.
To grab by a specific ID seems to be asking for trouble. The code below might be more akin to what you are looking for, BUT you will have to surround it with your own checks(eg What happens when you remove that node completely? I have had this happen before:) ) Below is yet another alternative:
Selecting a node from master template
I'm new to using Umbraco (I started a few days before V8 was launched) and I am struggling to work out the Razor syntax for selecting a specific node in my page structure when I'm on the master template.
My page structure looks like this:
I would like to select the Case Studies page from the master template so that I can list out the children of the case studies page.
This is what I have done on my master template (it works but I don't like it):
This feels like a hacky way to do it and I'm sure theres a better way, can anyone help?
Hey Jack
If it was me i would create a specific document type for my case studies and find the first descendant of my root page of that document type.
Alternatively you could create a property on the document type you have made for your frontpage with a Content Picker. Then in razor use the GetPropertyValue
Thanks for the advice, I tried accessing the first descendant of that document type but it wouldn't let me run .Children() on that variable that I stored it in.
you could do it with the new ugly way
or as Dennis said, create a new doc type for case studies and do something like those examples
Not sure what will work best in this version
Cheers
I got it to work with this which is better than my foreach loop:
Thanks for your help!
To grab by a specific ID seems to be asking for trouble. The code below might be more akin to what you are looking for, BUT you will have to surround it with your own checks(eg What happens when you remove that node completely? I have had this happen before:) ) Below is yet another alternative:
This is a great alternative which I have decided to implement, thanks for the help!
is working on a reply...