How do I access child / descendant node's properties and check if the field has a value
Hello all,
I am creating a hub page for Training Events.
This hub will present a list of all child nodes created below the TrainingHub doc type. I have done this:
var training = Model.Content.Children();
@foreach (var trainingEntry in training)
{
[html goes here]
}
However, how do I:
Query a specific field in a child-node (from the TrainingHub view) to see if it has any content in it, and output something (or not) based on this. For example:
var training = Model.Content.Children();
@foreach (var trainingEntry in training)
{
[html goes here]
@if (!string.IsNullOrEmpty( ))
{
[a view if the text field is not empty]
}
@else
{
[ a view if it is empty]
}
}
I don't know what the code would be to access the data in the child node from the parent (TrainingHub). Any idea how to get the data?
I tried the following but it didn't work:
var training = Model.Content.Children();
var trainingDateInput = Umbraco.Media(Model.Content.GetPropertyValue(training.TrainingMonthOne));
@if (!string.IsNullOrEmpty( trainingDateInput))
{
[a view if the text field is not empty]
}
@else
{
[ a view if it is empty]
}
I receive the error message:
'IEnumerable
I assume I have not queried it correctly. Please could you help.
Also, The same question above but for descendants of TrainingHub. I assume this is something like this to begin querying the data.
var trainingDates = Model.Content.DescendantOrSelf();
How do I access child / descendant node's properties and check if the field has a value
Hello all,
I am creating a hub page for Training Events.
This hub will present a list of all child nodes created below the TrainingHub doc type. I have done this:
However, how do I:
Query a specific field in a child-node (from the TrainingHub view) to see if it has any content in it, and output something (or not) based on this. For example:
I don't know what the code would be to access the data in the child node from the parent (TrainingHub). Any idea how to get the data?
I tried the following but it didn't work:
I receive the error message:
'IEnumerable
I assume I have not queried it correctly. Please could you help.
Also, The same question above but for descendants of TrainingHub. I assume this is something like this to begin querying the data.
See attached images for more info.
Thank you in advance. Ren.
Hi Ren,
You're on the right track getting the children.
To check if the child in the current iteration has a value in a property, you can use the 'HasValue' property from the IPublishedContent object.
See this post, it might help you. https://our.umbraco.org/forum/developers/razor/18303-How-do-I-use-IsNull-HasValue
Hello Louis,
Really appreciate the quick reply, so thank you.
I have to be honest, I'm new to Umbraco and I don't 100% know what to do with .HasValue or, more accurately, where to use / place it.
I followed the link and read the page, but it confused me even more :-(
I tried to use .HasValue and check for 'TrainingMonthOne' by doing this:
But I received a different error. It was:
Argument 2: cannot convert from 'object' to 'string'
Any ideas?
Regards Ren.
Hello all,
I just tested the dynamic version and it seems to work.
I tried this:
The output was:
Has content
no content
no content
no content
no content
no content
no content
Which is correct because information is only in one child-node field.
Which means I simply do not know what the correct command / code is for IPublishedContent - @Model.Content
I understand IPublishedContent is the best way to go because it allows you to use IntelliSense.
So, the question now is...could someone tell me what the correct code is for IPublishedContent, please. :-)
Would really appreciate it.
Regards, Ren.
Hi Ren.
You are very very close, here are a non-dynamic approach, based on your code:
This applies both for Model.Content.Children() and Model.Content.Descendants(), which should answer both question 1 and 2.
Hope this was helpful.
/ Dennis
Hi Ren,
Also ensure you're only prefixing code with a '@' e.g. your if and else statements when you need to.
This is usually only required when you've started some html tags. If you're in a code block
then you don't need to... but if you start some html..
This will ensure you avoid some confusing logic errors.
Steve
Hello all,
Thank you for getting in touch and helping to resolve the issue. I really appreciate it.
It all works!
Regards, Ren
Glad to help 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.