Copied to clipboard

Flag this post as spam?

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


  • steschu 57 posts 320 karma points
    1 week ago
    steschu
    0

    Get Media UmbracoHelper V8

    Hi,

    I am trying to migrate a v7 to a v8 site. This code line does not work anymore. How do I that with Umbraco 8? It is a partialView used by many Docs, al of which have a property introImage.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        var introImage = Umbraco.Media(Model.Value<string>("introImage"));
    }
    
    @if (introImage != null)
    {
            <img src="@introImage.GetCropUrl("2.5:1")" class="img-responsive" />
    }
    

    Kind regards, Stephan

  • Magnus Eriksson 80 posts 153 karma points
    1 week ago
    Magnus Eriksson
    1

    With ModelsBuilder/strongly typed models and using composition (?) for the property you should be able to do:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<INameOfComposition>
    @{
        var introImage = Model.IntroImage;
    }
    

    With Model.IntroImage giving you a Umbraco.Web.PublishedModels.Image.

    Regards, Magnus

  • Bjarne Fyrstenborg 1130 posts 3163 karma points MVP 3x c-trib
    1 week ago
    Bjarne Fyrstenborg
    101

    If you don't use ModelsBuilder, you can cast the value to an IPublishedContent to get the referenced media item.

    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        var introImage = Model.Value<IPublishedContent>("introImage"));
    }
    
    @if (introImage != null)
    {
            <img src="@introImage.GetCropUrl("2.5:1")" class="img-responsive" />
    }
    

    /Bjarne

  • steschu 57 posts 320 karma points
    1 week ago
    steschu
    0

    Thanks all

  • steschu 57 posts 320 karma points
    1 week ago
    steschu
    0

    Hi,

    found out, that the error I ran into is described here: https://our.umbraco.com/forum/umbraco-8/97235-possible-caching-error-umbracowebpublishedmodelsimage

    So, apprently not my old syntax was the reason, although it is good to know how to do a proper code for V8

Please Sign in or register to post replies

Write your reply to:

Draft