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.
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.
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
You can just cast the property to string:
This will return false if the property does not exist or has no value.
Ps. Instead of using the media API, you could just use the built in DynamicMedia functionality, and you can loose the using statement:
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.
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!
is working on a reply...