Copied to clipboard

Flag this post as spam?

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


  • Daniel Draper 36 posts 57 karma points
    Mar 25, 2011 @ 03:50
    Daniel Draper
    0

    Razor in MasterPage for checking properties

    Hi Everyone,

    I am checking if a property has a value on a MasterPage using the following code, while this code works it's not very elegant. Does anyone have a more elegant or better solutions?

    heroImage is a media picker property. I noticed if no node was selected then heroImage was a string however if a value was available it was an int which is why I have checked the type, slightly annoying.

        <umbraco:Macro language="razor" runat="server">
            @using umbraco.cms.businesslogic.media;

            @if(@Model.heroImage.GetType() != typeof(string)) {
                 Media m = new Media(@Model.heroImage);
                 <img src='@m.getProperty("umbracoFile").Value' alt="@Model.Name" width='@m.getProperty("umbracoWidth").Value' height='@m.getProperty("umbracoHeight").Value' />
            }
      </umbraco:Macro>

     


    Cheers,

    Daniel

     

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 25, 2011 @ 08:32
    Sebastiaan Janssen
    0

    You can just cast the property to string:

    @if(Model.heroImage.ToString() != "") {
     //do something
    }

    This will return false if the property does not exist or has no value.

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

    Ps. Instead of using the media API, you could just use the built in DynamicMedia functionality, and you can loose the using statement:

    @if(Model.heroImage.ToString() != "") {
       var m = Model.MediaById(Model.heroImage);
       <img src="@m.UmbracoFile" alt="@Model.Name" height="@m.UmbracoHeight" width="@m.UmbracoWidth" />
    }
  • Daniel Draper 36 posts 57 karma points
    Mar 28, 2011 @ 00:27
    Daniel Draper
    0

    Thanks, can't believe I didn't think about .ToString *headdesk*

    Thanks for the DynamicMedia option, been almost 12 months since I have worked on Umbraco however finally convinced the people that matter Umbraco is the way.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Mar 28, 2011 @ 08:03
    Sebastiaan Janssen
    0

    Hehe, no problem. We've all been there.

    The Razor implementation is really new so you can't be blamed for not knowing about it. Glad I could help!

Please Sign in or register to post replies

Write your reply to:

Draft