How do I display contents of doc type 'A' in a template not linked with doc type 'A', but linked doc type 'B'?
Hello all,
I'm new to Umbraco and I am really struggling to understand various concepts and I hoped someone could help me out.
The issue I am currently having is how to display properties from document type one (openingTimes), or its child nodes (openingTimesDays), and display them in a template (Haircuts.cshtml) which is not "linked" to doc type one (openingTimes)
It may make more sense if i describe the issue in connection with the picture.
I have a 'Haircuts' template, which presents information about various haircuts. The information presented in this template is taken from the 'Haircuts' child nodes - Gents, Ladies, Wash and Blow dry etc.
I have created another doc type (openingTimes) which has a number of child nodes below that - Monday, Tuesday etc (openingtimesDays).
How do I display the properties of the Monday, Tuesday etc. in the Haircuts template?
As I understand it, every item in the tree is linked at root and I need to traverse the tree, but I don't know how to do it.
It depends on which approach you are using for your templates, dynamic, strongly typed models or IPublished content.
But the principle is the same.
eg. from the Haircuts document type, you need to be able to find your 'openingTimes' document type, and then list out it's children.
You can get access to the root node of your site from within your template by using any of the following:
var [email protected](1);
var rootByDynamic = @CurrentPage.Site();
var rootByTypedContent = @Umbraco.TypedContentAtRoot().FirstOrDefault();
Once you have your root element, you can then look for descendants of type 'openingTimes'
//using IPublishedContent
var openingTimesByDescendants = rootByTraversing.Descendants().Where(f=>f.DocumentTypeAlias == "openingTimes").FirstOrDefault();
// using dynamic
var openingTimesByDynamicDescendants = rootByDynamic.Descendants().Where("DocumentTypeAlias == \"openingTimes\"").First();
//using Xpath
var openingTimesByXPath = Umbraco.TypedContentAtXPath("//openingTimes").FirstOrDefault();
Then when you have your openingTimes node you can list out the openingTimesDays elements as they are children of the openingTimes docType: eg something like this:
//using IPublishedContent
var openingTimesByDescendants = rootByTraversing.Descendants().Where(f=>f.DocumentTypeAlias == "openingTimes").FirstOrDefault();
is the 'Model' approach to look for decendants. Am I correct ?
Also, is the 'f' in f=>f.DocumentTypeAlias == "openingTimes" an arbitrary letter? Could it be p or b or any other letter?
I ask about the letter 'f' because I received a red squiggle in Visual Studio with the error being 'Cannot convert lamba expression to type 'string' because it is not a delegate type'
How do I display contents of doc type 'A' in a template not linked with doc type 'A', but linked doc type 'B'?
Hello all,
I'm new to Umbraco and I am really struggling to understand various concepts and I hoped someone could help me out.
The issue I am currently having is how to display properties from document type one (openingTimes), or its child nodes (openingTimesDays), and display them in a template (Haircuts.cshtml) which is not "linked" to doc type one (openingTimes)
It may make more sense if i describe the issue in connection with the picture.
I have a 'Haircuts' template, which presents information about various haircuts. The information presented in this template is taken from the 'Haircuts' child nodes - Gents, Ladies, Wash and Blow dry etc.
I have created another doc type (openingTimes) which has a number of child nodes below that - Monday, Tuesday etc (openingtimesDays)
.
How do I display the properties of the Monday, Tuesday etc. in the Haircuts template?
As I understand it, every item in the tree is linked at root and I need to traverse the tree, but I don't know how to do it.
Hi Ren
It depends on which approach you are using for your templates, dynamic, strongly typed models or IPublished content.
But the principle is the same.
eg. from the Haircuts document type, you need to be able to find your 'openingTimes' document type, and then list out it's children.
You can get access to the root node of your site from within your template by using any of the following:
Once you have your root element, you can then look for descendants of type 'openingTimes'
Then when you have your openingTimes node you can list out the openingTimesDays elements as they are children of the openingTimes docType: eg something like this:
or in dynamics
More info here: https://our.umbraco.org/documentation/Reference/Querying/ and https://our.umbraco.org/Documentation/Reference/Templating/Mvc/querying
Hello Marc,
Sorry, a quick questions about this.
var [email protected](1);
is the Models approach and I assume:
//using IPublishedContent var openingTimesByDescendants = rootByTraversing.Descendants().Where(f=>f.DocumentTypeAlias == "openingTimes").FirstOrDefault();
is the 'Model' approach to look for decendants. Am I correct ?
Also, is the 'f' in f=>f.DocumentTypeAlias == "openingTimes" an arbitrary letter? Could it be p or b or any other letter?
I ask about the letter 'f' because I received a red squiggle in Visual Studio with the error being 'Cannot convert lamba expression to type 'string' because it is not a delegate type'
The Dynamic approach obviously work.
Regards, Parm.
Hello Marc,
Result! Thank you so much.
I read, and re-read, the page you provided before posting, hoping to understand it, but it was tough.
I still struggle with what to use: dynamic, strongly typed models or IPublished content. As I don't fully understand the differences.
I will continue to work through the documentation. I'm sure (hope) it will click!
Many thanks.
Hi Ren,
Welcome to our forum!
Definitely do not use dynamics!
The best way if you are using visual studio is strongly typed models.
Thanks,
Alex
Hello Alex,
Cool, thanks for the advice. I will stick with Models.
Regards, Ren
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.