Copied to clipboard

Flag this post as spam?

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


  • olst 69 posts 88 karma points
    Nov 13, 2011 @ 18:14
    olst
    0

    check if a node has a property

    Hi.

    I'm trying to check if a certain node has a property "footerTextTitle" by:

    @foreach (var nodeLink in footerTop.ChildrenAsList.Where(n => n.GetProperty("footerTextTitle").IsNull() == true))
          {
            <li><a href='@nodeLink.GetProperty("footerLinkUrl").Value.ToString()'>@nodeLink.GetProperty("footerLinkTitle").Value.ToString()</a></li>
          }

    and getting a "Object reference not set to an instance of an object." error.

    How can I do this ?

  • Ismail Mayat 4511 posts 10092 karma points MVP 2x admin c-trib
    Nov 14, 2011 @ 10:36
    Ismail Mayat
    0

    olst,

    try !string.IsNullOrEmpty(n.footerTextTitle.ToString())

    Regards

    Ismail

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Nov 14, 2011 @ 10:43
    Jeroen Breuer
    0

    Have a look at the Razor DynamicNode Cheat Sheet. I think you can use these checks:

    .HasProperty(string propertyAlias)
    .HasValue(string propertyAlias)
    .IsNull(string propertyAlias)

    Jeroen

  • Adi 17 posts 36 karma points
    Nov 16, 2011 @ 16:48
    Adi
    0

    Have tried code like this

    Node somenode = new Node(myNodeID);


            if (somenode.GetProperty("myProperty") != null)
            {
                string myProperty = somenode.GetProperty("myProperty").Value.ToString();
                //Do something with myProperty
            }

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Nov 17, 2011 @ 19:17
    Sebastiaan Janssen
    1

    And for an even nicer and simpler and WAY more readable bit of code, try:

     

    @foreach (var item in footerTop.Children.Where("footerTextTitle != null && footerTextTitle.Trim() != string.Empty")) { 
    <li><a href="@item.footerLinkUrl">@item.footerTextTitle</a></li>
    }
Please Sign in or register to post replies

Write your reply to:

Draft