Copied to clipboard

Flag this post as spam?

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


  • Tom Cowling 144 posts 343 karma points
    Mar 10, 2015 @ 17:03
    Tom Cowling
    0

    Display an image

    I've never had this before and can't work out why it's happeneing..

    I have a standard setup wheremy template uses the @inherits Umbraco.Web.Mvc.UmbracoTemplatePage.

    I then have a seies of @sections throughout the page to drop in bits I need. Everything displays fine. What I would like to do is use a media picker to pull in an image from the media section. I have done this on another site easily enough, so thought I'll just recycle the code (below).

    <umbraco:Macro  runat="server" language="cshtml">
    @inherits umbraco.MacroEngines.DynamicNodeContext

            @{
            if (Model.HasValue("frontImage"))
                {                                                          
                    var selectedMedia = @Model.Media("frontImage");
                      <img class="headerimage" src="@selectedMedia.umbracoFile" width="@selectedMedia.umbracoWidth" height="@selectedMedia.umbracoHeight" alt="@selectedMedia.Name"/>
                }
            }
    </umbraco:Macro>

     

    For some reason this then breaks my page. The image pulls through fine, but the rest of the page show the razor code instead of rendering the items.

    This is how it's rendering on the page in plain text.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage @{ Layout = "Main.cshtml"; } @section Head { } @section Main {

    <The image appears here fine and exactly how I want it to >

    @Umbraco.Field("textArea")

    @Umbraco.Field("box1")
    @Umbraco.Field("box2")
    @Umbraco.Field("box3")
    Can anyone spot something obvious that I have missed? My brain just doesn't want to play ball today!
    Thanks,
    Tom
  • Tom Cowling 144 posts 343 karma points
    Mar 10, 2015 @ 17:07
    Tom Cowling
    0

    So to clarify, when I add in a macro, the template seems to ignore the fact it should be inheriting a master page from above using:

    @{
        Layout = "Main.cshtml";
    }

     

    And instead renders the macro and takes everything else as literal text..

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 10, 2015 @ 17:40
    Dennis Aaen
    100

    Hi Tom,

    Have you tried something like this. This should give you the picture from the media picker with an alias of frontImage, if the currentpage has any picture asigned.

    @if (CurrentPage.HasValue("frontImage")){                                         
        var dynamicMediaItem = Umbraco.Media(CurrentPage.frontImage);
            <img src="@dynamicMediaItem.umbracoFile" alt="@dynamicMediaItem.Name"/>
    }

    You can find the documentation for the media picker here: https://our.umbraco.org/documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors/Media-Picker If you are using Umbraco 7, then only look that the MVC View Example dynamic and strongly typed. The Razor Macro (DynamicNode & DynamicMedia) Example is old razor syntax which is deprecated. 

    Hope this helps,

    /Dennis

  • Tom Cowling 144 posts 343 karma points
    Mar 10, 2015 @ 17:45
    Tom Cowling
    0

    Perfect!

    I didn't realise it had been depreciated. I guess that's the problem running a few instances of Umbraco - I need to keep a log of what is transferable and what is not (some are on 6, some on 7)

    Thanks a lot for the quick response.

    871
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Mar 11, 2015 @ 08:17
    Dennis Aaen
    0

    Hi Tom,

    In addition to my post above, try to see my comment in this post https://our.umbraco.org/forum/umbraco-7/using-umbraco-7/59874-Razor-in-U7Am-I-just-over-tired?p=0#comment202799 about the different between the old DynamicNode razor that you have been used in Umbraco 6, and the MVC that is used in Umbraco 7.

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft