Copied to clipboard

Flag this post as spam?

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


  • Ren 8 posts 89 karma points
    Oct 15, 2016 @ 13:34
    Ren
    0

    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:

    1. 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.

    1. 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();
      

    See attached images for more info.

    Thank you in advance. Ren.enter image description here

    enter image description here

  • Louis Ferreira 69 posts 266 karma points
    Oct 15, 2016 @ 18:40
    Louis Ferreira
    0

    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

  • Ren 8 posts 89 karma points
    Oct 16, 2016 @ 16:25
    Ren
    0

    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:

      @foreach (var trainingEntry in training)
             {
               @if (!string.IsNullOrEmpty(Model.Content.HasValue(trainingEntry.GetPropertyValue("trainingMonthOne"))))
                   {
                           [code goes here ]             
    
                    }
    
              else
                {
                            [else code here]
                }
    
            }
    

    But I received a different error. It was:

    Argument 2: cannot convert from 'object' to 'string'

    Any ideas?

    Regards Ren.

  • Ren 8 posts 89 karma points
    Oct 16, 2016 @ 17:26
    Ren
    0

    Hello all,

    I just tested the dynamic version and it seems to work.

    I tried this:

     var trainingTest = CurrentPage.Children();
     @foreach (var test in trainingTest)
                    {
                        if (!string.IsNullOrEmpty((test.trainingMonthOne)))
                        {
                            <p>Has content</p>
                        }
                        else
                        {
                            <p>no content</p>
                        }
                    }
    

    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.

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Oct 17, 2016 @ 06:15
    Dennis Adolfi
    100

    Hi Ren.

    You are very very close, here are a non-dynamic approach, based on your code:

    var training = Model.Content.Children();  
        @foreach (var trainingEntry in training)
                 {
                   @if (trainingEntry.HasValue("trainingMonthOne"))
                       {
                               [code goes here ]                 
                        }    
                  else
                    {
                                [else code here]
                    }    
                }
    

    This applies both for Model.Content.Children() and Model.Content.Descendants(), which should answer both question 1 and 2.

    Hope this was helpful.

    / Dennis

  • Steve Morgan 1350 posts 4460 karma points c-trib
    Oct 17, 2016 @ 09:34
    Steve Morgan
    0

    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

    @{
         // code block here 
    }
    

    then you don't need to... but if you start some html..

        @{
             // note if does not need @if but the var does
            if(somethingistrue)
            {
                <div>
    
                    @varToOutput 
                </div>
            }
            else {
                // no need here either
            }
        }
    

    This will ensure you avoid some confusing logic errors.

    Steve

  • Ren 8 posts 89 karma points
    Oct 19, 2016 @ 21:40
    Ren
    1

    Hello all,

    Thank you for getting in touch and helping to resolve the issue. I really appreciate it.

    It all works!

    Regards, Ren

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    Oct 20, 2016 @ 13:48
    Dennis Adolfi
    0

    Glad to help Ren!! :)

  • 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.

Please Sign in or register to post replies