Copied to clipboard

Flag this post as spam?

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


  • David Hyslop 22 posts 155 karma points
    Oct 12, 2017 @ 12:16
    David Hyslop
    0

    Get URL of Media Picker Image

    Hi

    If anyone could offer any help would be greatly appreciated. This one is driving me mad.

    I'm displaying some child pages in a template and have an image in the pages that I'd like to display and am strggling getting the Url of that image.

        @{
                    var navitem = CurrentPage.Children.Where("Visible");
    
    
                }
    
                @{foreach (var item in navitem)
    
                    {
                        <div class="col-xs-4">
                            <div class="news-item">
    
                                <h2> @item.pageTitle</h2>
                                <p> <i class="fa fa-calendar" aria-hidden="true">@item.UpdateDate.ToString("dd MMMM yyyy")</i><br /></p>
                                <p class="news-intro">
    
                                    @item.introText
    
                                </p>
    
                           <img src="@item.newsImage">
                            </div>
                        </div>
                    }
    
                }
    

    Its @item.newsImage where I'm just getting the ID. Does anyone know how to convert that to the URL.

    Thanks.

  • Alex Skrypnyk 4282 posts 15451 karma points MVP
    Oct 12, 2017 @ 12:20
    Alex Skrypnyk
    100

    Hi David

    Use this code:

    <img src="@Umbraco.Media(item.newsImage).Url">
    

    Also be aware that dynamics isn't the best practice for using.

    Thanks,

    Alex

  • David Hyslop 22 posts 155 karma points
    Oct 12, 2017 @ 12:40
    David Hyslop
    0

    Thanks Alex, little change but worked fine.

      @{int newsImageId = item.newsImage;
                                }
    
                                <img src="@Umbraco.Media(newsImageId).Url">
    
  • Alex Skrypnyk 4282 posts 15451 karma points MVP
    Oct 12, 2017 @ 13:17
    Alex Skrypnyk
    0

    Glad to help you!

    Have a great evening!

  • hassanisback 8 posts 78 karma points
    14 days ago
    hassanisback
    0

    Hi Alex and David I got the same problem but the solution doesn't work for me as I am using this ImageId in another page (partial view). I am getting the value from template

    @foreach(var item in selection){
    
      <section >
      @Html.Partial("/Views/Partials/Sections/" + @item.SectionType + ".cshtml", new ViewDataDictionary {
            {"text",   @item.PageContent},
            {"image", @item.Image},
            {"heading", @item.Heading}
    
        })
      </section>  
      }
    

    and passing it to a partial view

    var imageUrl = ViewData["image"];
    <div class="col-md-5 text-center"><img class="content-section-img" src="@imageUrl" alt=" " />
            </div>
    

    Any idea? Thanks

Please Sign in or register to post replies

Write your reply to:

Draft