Copied to clipboard

Flag this post as spam?

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


  • blackhawk 313 posts 1368 karma points
    Dec 13, 2017 @ 14:10
    blackhawk
    0

    ModelsBuilder - Creating short conditions

    I'm on Umbraco 7.7.2 with ModelsBuilder enabled.

    I'm trying to learn how to check if if a property is not empty through the use of ModelsBuilder. This was my initial attempt...

    @if (Model.GlobalContentPanel != null)
            {
                @Model.GlobalContentPanel;
            }
    

    If that looks correct, is there a way to shorten that into a single line like this...

     @(Model.HasValue("globalContentPanel") ? Model.GlobalContentPanel : "<p>nothing to show at the moment</p>");
    

    At the moment, Visual Studio is complaining about this attempt.

    Thanks for any insight

  • Dan Diplo 1554 posts 6205 karma points MVP 6x c-trib
    Dec 13, 2017 @ 14:33
    Dan Diplo
    0

    What type is Model.GlobalContentPanel ?

    If, for instance, it was a string, then you could just do:

    var content = !String.IsNullOrEmpty(Model.GlobalContentPanel) ? Model.GlobalContentPanel : "<p>nothing to show at the moment</p>");
    

    But if it's an IHtmlString (as returned by a rich-text property), then you'd check for a null:

    var content = Model.GlobalContentPanel ?? new HtmlString("<p>nothing to show at the moment</p>");
    

    Or you can check for empty (but not null) HTML by doing:

    var content = !String.IsNullOrEmpty(Model.GlobalContentPanel.ToString()) ? Model.GlobalContentPanel : new HtmlString("<p>nothing to show at the moment</p>")
    

    If it's a different type, let us know...

  • blackhawk 313 posts 1368 karma points
    Dec 13, 2017 @ 14:41
    blackhawk
    0

    ahh I didn't know any of this. My property is a RTE. and I simply want to check if its empty, and set my backup html text, if the value is empty.

    I attempted to do the following (to test by backup text)...

        @{
            var mcontent = Model.GlobalContentPanel ?? new HtmlString("<p>nothing to show at the moment</p>");
        }
        @mcontent
    

    ...but with no content, it does not display nothing to show at the moment on my page. its just blank.

  • blackhawk 313 posts 1368 karma points
    Dec 13, 2017 @ 16:08
    blackhawk
    100

    I believe I found a working solution....

      @(Model.HasValue("globalContentPanel") ? Model.GlobalContentPanel : new HtmlString("nothing to show at the moment"));
    

    By default this will load my RTE content. But if empty, will display my backup HtmlString.

    Thanks for helping me think this through!

Please Sign in or register to post replies

Write your reply to:

Draft