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
    Mar 12, 2011 @ 19:23
    Sebastiaan Janssen
    0

    How do I use IsNull / HasValue

    I'm trying to test if a property exists on my document type:

    Model.PageTitle.IsNull

    And this works great if the property PageTitle does not exist. However, if it does, it will be cast to string and I get and error ('string' does not contain a definition for 'IsNull').

    So how am I supposed to prevent errors from the property not existing, without getting errors when the property DOES exist?

    I've also tried HasValue, but apparently: 'string' does not contain a definition for 'HasValue'

     

     

  • Gareth Evans 143 posts 335 karma points c-trib
    Mar 13, 2011 @ 02:49
    Gareth Evans
    0

    In 4.7rc, use HasProperty; so: Model.HasProperty("PageTitle")

    In 4.7 [final], when a property doesn't exist, DynamicNull will be returned, so you can use one of these three:

    1) Model.someProperty.GetType() == typeof(DynamicNull)

    2) Model.someProperty.IsNull() [true]

    3) Model.someProperty.HasValue() [false]

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 13, 2011 @ 12:10
    Sebastiaan Janssen
    0

    2 & 3 That doesn't seem to work as expected:

    @Model.someProperty.HasValue

    Gives me false - which is correct

    Then 

    @Model.someProperty.HasValue()

    Gives an error: Cannot invoke a non-delegate type

    Also, when the property does exist:

    @Model.PageTitle.HasValue

    (also tried with HasValue() ) 

    Gives me the error: 'string' does not contain a definition for 'HasValue'

    The same behaviour occurs when using IsNull.

    The only thing that works reliably is using the first method (GetType()).

    So right now HasValue and IsNull can only be used when you're sure that the property does NOT exist, which is backwards.. ;-)

    I was using the RC: 1.0.4087.21620 - changeset: 7f84eb55e573

    Just to make sure, I upgraded to the latest changeset: 1.0.4089.21758 - changeset: c2a5ffd2c4e7  

    But the behaviour is the same still.

  • Gareth Evans 143 posts 335 karma points c-trib
    Mar 13, 2011 @ 21:11
    Gareth Evans
    0

    They are most likely getters instead of methods, which means my first post was wrong.

    I will need to implement IsNull and HasValue on DynamicNode as well as DynamicNull, to allow you to use it regardless of if you get a DynamicNull or not.

    This is a really quick patch, in fact, i'll go get my laptop and do it and commit it in the next 30 min, so you can retest. Don't know when the final is coming out and would like this one fixed

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 13, 2011 @ 21:18
    Sebastiaan Janssen
    0

    Excellent! Final coming out in about 12-24 hours according to Niels on Twitter just now! :)

  • Gareth Evans 143 posts 335 karma points c-trib
    Mar 13, 2011 @ 21:20
    Gareth Evans
    0

    I just realised, if you have a native type, e.g. string, i'm going to have to put them on there as extension methods - to get the seemless test like you're after..

    Shouldn't be too difficult, there's only about 6 or 7 possible types at the moment

  • Gareth Evans 143 posts 335 karma points c-trib
    Mar 13, 2011 @ 21:20
    Gareth Evans
    0

    Did .HasProperty(string) work for you ?

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 13, 2011 @ 21:37
    Sebastiaan Janssen
    0

    Ah yes, that works fine, that's a good check as well. But I would like it if HasValue would check if the prop exists AND has a value, as you will want to do that a lot of the time (a bit like string.IsNullOrEmpty).

  • Gareth Evans 143 posts 335 karma points c-trib
    Mar 13, 2011 @ 21:45
    Gareth Evans
    0

    Have been playing around, don't think it's going to be possible

    I can't get the extension methods to be available on the types (string, etc) from razor - (and i had to change them to be methods not getters)

    It's easy enough to add them to my own types (DynamicNode, DynamicMedia etc) but when you have an actual property, it's going to be a native type

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 13, 2011 @ 21:47
    Sebastiaan Janssen
    0

    Shame! But .HasProperty(string) works great, so I'll stick with that. It's much easier to read and understand than the GetType() version anyway.

Please Sign in or register to post replies

Write your reply to:

Draft