Copied to clipboard

Flag this post as spam?

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


  • Bobi 346 posts 950 karma points
    Mar 04, 2020 @ 18:54
    Bobi
    0

    How to write a conditional statement with empty property alias

    What is the recommended approach for writing a conditional statement with a property alias? For example, if you have a text string property alias called propertyAlias and you want to do nothing if the property alias has no text, but do something else if the property alias has text, what would be the approach?

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Mar 04, 2020 @ 19:20
    Marc Goodson
    0

    Hi Bobi

    An IPublishedContent item has a 'HasValue' method that you can check first before doing something with the property value:

    @if (Model.HasValue("title")){
    <h2>@(Model.Value<string>("title"))</h2>
    }
    else {
    <!-- don't do anything here, no title has been entered -->
    }
    

    is that what you are after?

    regards

    Marc

  • Bobi 346 posts 950 karma points
    Mar 05, 2020 @ 16:45
    Bobi
    0

    I think so. I will get back to you to mark this as the solution if it works. Just implementing now.

  • Bobi 346 posts 950 karma points
    Mar 21, 2020 @ 18:21
    Bobi
    0

    Hi, Marc, was there a reason you didn't use @Model.Value("string") instead of @(Model.Value

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Mar 21, 2020 @ 20:02
    Marc Goodson
    0

    Hi Bobi

    Yes, largely out of habit

    if you write

    Model.Value<string>("title")
    

    You are requesting a 'strongly typed version' of the property, in this case a string... but if it were a 'Media Picker'

    Model.Value<IPublishedContent>("mediaPickerAlias")
    

    you would get back an IPublishedContent object representing the picked media

    or

    Model.Value<IEnumerable<IPublishedContent>>("mediaPickerAlias")
    

    if it was a multimedia picker... basically something called a PropertyValueConverter in the core of Umbraco (you can write your own too for custom property editor types) is doing the conversion from the stored value to something nice to work with in your view.

    When you just use

    @Model.Value("title")

    then the type returned is 'object' - if all you are doing is writing it out directly into the View, then there is no difference between

    @Model.Value("title")
    

    and

    @(Model.Value<string>("title"))
    

    but if you are assigning the value to a variable to manipulate it further, or you have a complex type (eg the media picker scenario) then the 'strongly typed' approach can save effort...

    ... why the extra brackets?

    well because the strongly typed <string> bit looks like a html tag.. it can confuse the razor view syntax checker, putting it inside a @() just tells the parser that this is code, and not html...

    regards

    Marc

Please Sign in or register to post replies

Write your reply to:

Draft