Copied to clipboard

Flag this post as spam?

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


  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 25, 2011 @ 13:50
    Sebastiaan Janssen
    0

    How can I test if a property exists?

    I am trying to call a property that may or may not be defined for the current document type: Model.PageTitle

    When it's not defined, I get "'umbraco.MacroEngines.DynamicNode' does not contain a definition for 'PageTitle'". I've tried testing if Model.PageTitle is null, but that didn't help. The only way I've found to hide the error is throwing it in a try-catch block, which isn't ideal.

    Any better way?

  • Gareth Evans 143 posts 335 karma points c-trib
    Feb 25, 2011 @ 14:38
    Gareth Evans
    1

    In the final, if the property isn't fetched, I return an empty DynamicNodeList

    The cause of the bug is similar to the reason that .nodeTypeAlias crashes when there's no nodes

    I don't like to return null in DynamicNode because it breaks chaining. One null return and your whole razor file will stop with a cannot invoke on a null reference (or something similar to that)

    I'll add a HasProperty("propertyName") => bool helper

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 25, 2011 @ 16:09
    Sebastiaan Janssen
    0

    Excellent, that is the best I could hope for! And you're very correct about returning nulls, solution sounds great.

Please Sign in or register to post replies

Write your reply to:

Draft