Copied to clipboard

Flag this post as spam?

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


  • David Gregory 87 posts 233 karma points
    Mar 09, 2023 @ 13:28
    David Gregory
    0

    Model.HasValue

    Umbraco 10.1.0

    Am I missing something simple here? The first if doesn't work but the second one does on this textBox property.

            @if (Model.HasValue(Model.WelcomeBoxTitle))
            {
                <h1>@Model.WelcomeBoxTitle (1)</h1>
            }
    
            @if (Model.HasValue("welcomeBoxTitle"))
            {
                <h1>@Model.WelcomeBoxTitle (2)</h1>
            }
    
  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Mar 09, 2023 @ 13:43
    Huw Reddick
    0

    the second one is correct since the HasValue is to check if the property exists on the IPublishedContent and expects the property alias

  • David Gregory 87 posts 233 karma points
    Mar 09, 2023 @ 14:30
    David Gregory
    0

    Ah I see.

    Is there a way then to check if a property has content using the models builder version?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Mar 09, 2023 @ 14:37
    Huw Reddick
    0

    HasValue will return false if the property itself is empty.

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Mar 09, 2023 @ 14:38
    Huw Reddick
    0

    sorry for confusion, my first comment was not 100% accurate.

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Mar 09, 2023 @ 14:44
    Huw Reddick
    0

    so @if (Model.HasValue("welcomeBoxTitle")) will be false if the property does not exist or is empty

  • David Gregory 87 posts 233 karma points
    Mar 09, 2023 @ 14:49
    David Gregory
    0

    Yes, that one works for me, but this one always returns false regardless i.e if there is content in the property or not.

    @if (Model.HasValue(Model.WelcomeBoxTitle))
            {
                <h1>@Model.WelcomeBoxTitle (1)</h1>
            }
    
  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Mar 09, 2023 @ 15:04
    Huw Reddick
    1

    That is because Model.WelcomeBoxTitle is not the property alias, and hasvalue requires the property alias

Please Sign in or register to post replies

Write your reply to:

Draft