Copied to clipboard

Flag this post as spam?

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


  • steschu 88 posts 489 karma points
    Jun 14, 2019 @ 08:51
    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 122 posts 362 karma points
    Jun 14, 2019 @ 09:54
    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 1282 posts 3994 karma points MVP 8x c-trib
    Jun 14, 2019 @ 10:15
    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 88 posts 489 karma points
    Jun 14, 2019 @ 13:31
    steschu
    0

    Thanks all

  • steschu 88 posts 489 karma points
    Jun 15, 2019 @ 09:06
    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