Copied to clipboard

Flag this post as spam?

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


  • Brian Kreck 9 posts 92 karma points
    Mar 05, 2018 @ 01:29
    Brian Kreck
    0

    Hopefully simple Umbraco / C# / RAZOR question...

    I have a silly question that is as much a simple RAZOR / C# question as an Umbraco one, but I've looked for answers for quite some time and can't seem to find any.

    My goal is to:

    • Test to see if a field in Umbraco has been provided... let's say check to see if an image has been provided via the media picker.
    • Throughout the view, display content when it has been provided and skip the block when it hasn't.

    This is easy when one can live within an IF block, for example:

    if (Model.Content.HasValue("myImage") {
      iPublishedContent myImage = Umbraco.TypedMedia(Model.Content.GetProperValue("myImage"));
        <div>
            <img src="@myImage.GetPropertyValue("umbracoFile")"> //etc.
        </div>
    }
    

    However, in complex layouts, let's say when the presence of that image changes the layout throughout the page, this method becomes terribly ugly. What I'd like to do is instantiate myImage early and then easily test later if it has a value.

    Here's what I've explored and rejected:

        if (Model.Content.HasValue("myImage") {
          iPublishedContent myImage = Umbraco.TypedMedia(Model.Content.GetProperValue("myImage"));
        }
       if (myImage!=null) {
            <div>
                <img src="@myImage.GetPropertyValue("umbracoFile")"> //etc.
            </div>
       }
    

    This doesn't work as myImage looses scope when leaving the IF block (as expected)...

    IPublishedContent? myImage;    
    if (Model.Content.HasValue("myImage") {
          iPublishedContent myImage = Umbraco.TypedMedia(Model.Content.GetProperValue("myImage"));
        }
       if (myImage!=null) {
            <div>
                <img src="@myImage.GetPropertyValue("umbracoFile")"> //etc.
            </div>
       }
    

    Doesn't work because IPublishedContent isn't nullable.

    IPublishedContent myImage;    
    if (Model.Content.HasValue("myImage") {
          iPublishedContent myImage = Umbraco.TypedMedia(Model.Content.GetProperValue("myImage"));
        }
       if (myImage!=null) {
            <div>
                <img src="@myImage.GetPropertyValue("umbracoFile")"> //etc.
            </div>
       }
    

    Returns a 'Use of unassigned local variable' error.

    IPublishedContent myImage;
    bool myImageDefined = false;   
    if (Model.Content.HasValue("myImage") {
          iPublishedContent myImage = Umbraco.TypedMedia(Model.Content.GetProperValue("myImage"));
         myImageDefined = true;
        }
       if (myImageDefined) {
            <div>
                <img src="@myImage.GetPropertyValue("umbracoFile")"> //etc.
            </div>
       }
    

    Same error... "Use of unassigned local variable"

    Any ideas or tricks to accomplish this?

    Thanks again for your time!

    Brian

  • Nathan Woulfe 447 posts 1664 karma points MVP 5x hq c-trib
    Mar 05, 2018 @ 05:25
    Nathan Woulfe
    100

    Hi Brian

    IPublishedContent isn't nullable, you're right, but it's value can be null.

    So something like this should work:

    IPublishedContent myImage = null;
    if (Model.Content.HasValue("myImage")) {
        // assign to myImage
    }
    
    if (myImage != null) {
        // do stuff
    }
    
    // more stuff
    
    if (myImage != null) {
        // do other stuff
    }
    
  • Brian Kreck 9 posts 92 karma points
    Mar 05, 2018 @ 17:47
    Brian Kreck
    0

    Thank you very much! I suspected it was a simple fix. I had a misunderstanding on what "nullable" meant. :-)

    I really appreciate it.

    • Brian
Please Sign in or register to post replies

Write your reply to:

Draft