Copied to clipboard

Flag this post as spam?

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


  • Martin 278 posts 662 karma points
    Sep 18, 2015 @ 12:36
    Martin
    0

    Multi If Statement for Document Type Alias

    Hi,

    Im trying apply a navigation structure depending on the document type alias used.

    Im checking if the pages have any children, and then the doc type alias.

    Im running in to problems with duplicate nodes.

    Any help would be grateful.

    Martin

     @foreach (var childPage in home.Children.Where("Visible")) {
    if (childPage.Children.Any()) {
        if(childPage.DocumentTypeAlias == "DocType-1" ) { <li class=""><a href="">Doc Type 1</a></li>
        }
    
        if(childPage.DocumentTypeAlias == "DocType-2" ) { <li class=""><a href="">Doc Type 2</a></li>
        }
    
        else { <li class=""><a href="">Any Doc Type</a></li>
        }
    }
    else { <li class=""><a href="">Any Doc Type</a></li>
    } }
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Sep 18, 2015 @ 13:06
    Dennis Aaen
    0

    Hi Martin,

    Could you try this code and see if it works like you wanted it to work.

    @{ 
        var home = CurrentPage.AncestorOrSelf(1);
    
        foreach (var childPage in home.Children.Where("Visible")) {
            if (childPage.Children.Any()) {
                if(childPage.DocumentTypeAlias == "DocType1Alias" ) { 
                    <li class=""><a href="">Doc Type 1</a></li>
    
                    }
    
                if(childPage.DocumentTypeAlias == "DocType2Alias" ) { 
                    <li class=""><a href="">Doc Type 2</a></li>
                }
    
                else { 
                    <li class=""><a href="">Any Doc Type</a></li>
                }
            }
            else { 
                <li class=""><a href="">Any Doc Type</a></li>
            } 
    
        }   
    } 
    

    Hope this helps,

    /Dennis

  • Ajay Karwal 31 posts 149 karma points
    Sep 18, 2015 @ 13:08
    Ajay Karwal
    100

    Reformating your code for readability, you're probably getting the duplicates due to the 2 if statements

    @foreach (var childPage in home.Children.Where("Visible")) {
    
      if (childPage.Children.Any()) {
          if(childPage.DocumentTypeAlias == "DocType-1" ) { 
            <li class=""><a href="">Doc Type 1</a></li>
          }
    
          if(childPage.DocumentTypeAlias == "DocType-2" ) {
            <li class=""><a href="">Doc Type 2</a></li>
          } else {
            <li class=""><a href="">Any Doc Type</a></li>
          }
      } else { 
        <li class=""><a href="">Any Doc Type</a></li>
      }
    
    }
    

    Now consider your doctype is DocType-1. That would satisfy TURE for the first IF

    if(childPage.DocumentTypeAlias == "DocType-1" )  {
      <li class=""><a href="">Doc Type 1</a></li>
    }
    

    and also be rendered in the ELSE

    if(childPage.DocumentTypeAlias == "DocType-2" )  {
      <li class=""><a href="">Doc Type 2</a></li>
    } else {
      <li class=""><a href="">Any Doc Type (can also be Doc Type 1 again)</a></li>
    }
    

    Hence the double result.

    Changing you're whole code block to this should fix things.

    @foreach (var childPage in home.Children.Where("Visible")) {
    
      if (childPage.Children.Any()) {
          if(childPage.DocumentTypeAlias == "DocType-1" ) { 
            <li class=""><a href="">Doc Type 1</a></li>
          } else if(childPage.DocumentTypeAlias == "DocType-2" )  {
            <li class=""><a href="">Doc Type 2</a></li>
          } else {
            <li class=""><a href="">Any Doc Type</a></li>
          }
      } else { 
        <li class=""><a href="">Any Doc Type</a></li>
      } 
    
    }
    

    Also, the last else {} probably isn't needed. What would that render?

  • Martin 278 posts 662 karma points
    Sep 18, 2015 @ 14:42
    Martin
    0

    Thanks Ajay.

  • Carlos Mosqueda 240 posts 431 karma points
    Oct 26, 2021 @ 18:40
    Carlos Mosqueda
    0

    Resurrecting this one for Umbraco 7.15 at least (yep I know U8 and U9 are out), and maybe U8 will work too. The "IsDocumentType()" method worked nicely for me.

    if(childPage.IsDocumentType("doctype-1") ) {
    //Do something here
    }else if(childPage.IsDocumentType("doctype-1") ) {
    //Do something here
    }else{//blah}
    
Please Sign in or register to post replies

Write your reply to:

Draft