Copied to clipboard

Flag this post as spam?

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


  • DonZalmrol 220 posts 833 karma points
    Jun 11, 2021 @ 09:34
    DonZalmrol
    0

    Get image url from selection (V8) not working

    Hi all,

    Finally trying out v8 and testing bit by bit to move over my active site. However when I try to get the image URL from a simple selection of my featureItemsPage.

    The image is selected with the media picker data type.

        @inherits Umbraco.Web.Mvc.UmbracoViewPage
    @{
        var selection = Umbraco.Content(Guid.Parse("pageGUIDHere"))
        .ChildrenOfType("featureItem").OrderBy(x => x.Name);
    }
    
    <div id="features-wrapper">
        <div class="container">
            <div class="row">
                @foreach (var item in selection)
                {
                <div class="col-4 col-12-medium">
    
                    <!-- Box -->
                    <section class="box feature">
                        @if(item.HasValue("featureImage"))
                        {
                            var image = item.Value<IPublishedContent>("featureImage");
                            <a href="#" class="image featured"><img src="@image.Url" alt="" /></a>
                        }
                        <div class="inner">
                            <header>
                                <h2>@item.Value("featureTitle")</h2>
                                @item.Value("featureSubTitle")
                            </header>
    
                            @item.Value("featureText")
    
                        </div>
                    </section>
    
                </div>
                }
            </div>
        </div>
    </div>
    

    I get the following error: Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

    Line 18:                    @if(item.HasValue("featureImage"))
    Line 19:                     {
    Line 20:                        var image = item.Value<IPublishedContent>("featureImage");
    Line 21:                        <a href="#" class="image featured"><img src="@image.Url" alt="" /></a>
    Line 22:                    }
    

    Probably a simple issue and V8 newbie mistake.

    Thx!

  • shekhar sharma 22 posts 110 karma points
    Jun 11, 2021 @ 10:39
    shekhar sharma
    0

    Hello DonZalMrol ,

    @if(item.HasValue("featureImage")) {
    var image = item.Value

    Can you please try Below..!!!!!!

    @if(item.HasValue("featureImage"))

         {        
            var image = item.Value<IPublishedContent>("featureImage");
         <a href="#" class="image featured"><img src="@image.Url()" alt=""</a>              
    }
    

    Just Add Close () in the end @image.Url()

    If Above not work Try Below

    var image = item.Value< MediaWithCrops >("featureImage");

    item.Value< MediaWithCrops >("FacebookImage").Url()

    Replace IpublishContent With Media With Crops.

    Thanks,

    Shekhar

  • DonZalmrol 220 posts 833 karma points
    Jun 11, 2021 @ 13:24
    DonZalmrol
    0

    Hi Shekhar, thanks! Unfortunately, no luck :(

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Jun 14, 2021 @ 06:49
    Huw Reddick
    100

    Are you using the old MediaPicker or the latest MediaPicker 3?

    with the new MediaPicker you need to do

    var image = item.Value<MediaWithCrops>("featureImage");
    and to get the url use image.MediaItem.Url();
    
  • DonZalmrol 220 posts 833 karma points
    Jun 14, 2021 @ 11:27
    DonZalmrol
    0

    After changing the code to your suggestion the page now loads with the image!

    Thanks!

  • Huw Reddick 1932 posts 6722 karma points MVP 2x c-trib
    Jun 14, 2021 @ 13:24
    Huw Reddick
    1

    No problem, had the same issue myself over the weekend when i changed to the new media picker :D

  • DonZalmrol 220 posts 833 karma points
    Jun 14, 2021 @ 14:23
    DonZalmrol
    0

    Ha :D

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies