Copied to clipboard

Flag this post as spam?

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


  • Ault Nathanielsz 87 posts 407 karma points c-trib
    May 24, 2018 @ 21:51
    Ault Nathanielsz
    0

    NewB - Following guide, pulling out hair

    First template worked great. Then tried displaying an image.

    I have reduced it to the most basic code possible - trying to work from the Documentation, and getting nowhere.

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        Layout = null;
    }
    
    @{
        var mediaItem = Umbraco.TypedMedia("1057").OfType<Image>();    
    }  
    <img src="@mediaItem.URL" />
    

    gives me

    Compiler Error Message: CS1061: 'Umbraco.Web.PublishedContentModels.Image' does not contain a definition for 'URL' and no extension method 'URL' accepting a first argument of type 'Umbraco.Web.PublishedContentModels.Image' could be found (are you missing a using directive or an assembly reference?)

    Obviously there is something missing that the documentation has omitted. Any assistance much appreciated.

  • Nigel Wilson 944 posts 2076 karma points
    May 25, 2018 @ 03:12
    Nigel Wilson
    0

    Hey

    Try "Url"

    Cheers

    Nigel

  • Ault Nathanielsz 87 posts 407 karma points c-trib
    May 25, 2018 @ 09:56
    Ault Nathanielsz
    0

    That would be one issue (thanks for spotting it)... but it just raises another:

    Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Source Error:

    Line 9: @{ Line 10: Line 11: var mediaItem = Umbraco.TypedMedia("1057").OfType();
    Line 12: }
    Line 13: < img src="@mediaItem.Url" />

    As I have copy/pasted the media ID straight out of the image's info panel (and it displays if placed into a rich text editor)...

    Any thoughts?

  • Nigel Wilson 944 posts 2076 karma points
    May 25, 2018 @ 18:06
    Nigel Wilson
    0

    Hey Ault

    Have you tried just:

    var mediaItem = Umbraco.TypedMedia(1057);
    

    or

    var mediaItem = Umbraco.Media(1057);
    

    Cheers

    Nigel

  • Ault Nathanielsz 87 posts 407 karma points c-trib
    May 29, 2018 @ 14:10
    Ault Nathanielsz
    100

    Hi Nigel

    Both of those result in an empty string. I have added another image to the media library and got the same result...

    HOWEVER.. while typing this I realise that when setting up, I created a new media type: MyImage and used that. Better go make sure that hasn't messed things up...


    BINGO!!!

    A few tests later... That was the issue.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    May 25, 2018 @ 18:39
    Jan Skovgaard
    0

    Hi Ault

    Since you inherit from the UmbracoTemplatePage you can get your media by using the below code snippet, which Dave Woestenborghs shows in this article https://24days.in/umbraco-cms/2015/strongly-typed-vs-dynamic-content-access/ - It gives a nice explanation on how to do things whether you're using strongly typed or dynamics. Yes there currently is 2 different ways of achieving the same end result. I'll spare you the dull technical details but it is confusing to figure out especially when one is just starting out and want to render things :-)

    Anyhow let's get back to the snippet from Dave's article - It looks like this.

    @if(**CurrentPage.Image** != null)
    {
        var mediaId = **CurrentPage.Image**;
        var mediaItem = Umbraco.Media(mediaId);
    
        if(mediaItem != null)
        {           
            <p><img src="@mediaItem.Url" alt="" /></p>    
        }
    }
    

    So the in the if check where it says "CurrentPage.Image" you should replace the "Image" part with with the alias of your media property if it's called something else like "Media" or "BackgroundImage" etc.

    You should also do this in the line where he is setting the mediaId variable. I have highlighted those parts of the code.

    As you can see there is a check to make sure that a media item has actually been picked by an editor in the umbraco backoffice. If there was no check for this then it could result in one of those cryptic yellow screens of death (YSOD's) that you have already experienced. So we can avoid this by making sure there is a value before we try to render the image.

    If a media item has been selected we get the id of the media, which we can then pass to Umbraco.Media, which will give us back the media object where the Url among other properties can now be called for showing the media.

    Now this should output your image.

    I hope my explanation above makes sense otherwise feel free to ask further questions - And I also recommend that you read Dave's articles a couple of times since it's gives some explanation to the different approaches that can be used to render the Umbraco data. Another source of information is this page from the documentation here https://our.umbraco.org/documentation/reference/templating/mvc/views

    Cheers, Jan

  • Ault Nathanielsz 87 posts 407 karma points c-trib
    May 29, 2018 @ 14:19
    Ault Nathanielsz
    0

    Hi Jan,

    Thanks for the comprehensive reply. (My problem - see reply to Nigel above - lay elsewhere...)

    I had seen the 24Days series previously. One of my problems with it (and many other Umbraco articles) is that it does not feature a date - so it is difficult to establish how relevant the text is to the current version.

    (Although one can infer that it is pre-7.4 from the mentions about Models Builder shipping with future versions.)

    Thanks again, Ault

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    May 29, 2018 @ 14:26
    Jan Skovgaard
    0

    Hi Ault

    Happy to hear you managed to solve your issue :-)

    Hmm, yeah I see that it's not very explicit when one is visiting one or more of the articles out of season. But we do mark the day with 4 and the year is a part of the url. Which makes good sense once the calendar is running during the month of december but might not be very clear when it's out of season. That's a good point - I'll file an issue in our basecamp so we can discuss if we should write the date more clearly somewhere.

    Thanks for the feedback - Happy coding!

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft