Copied to clipboard

Flag this post as spam?

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


  • Eduardo Rivas 17 posts 37 karma points
    Feb 19, 2012 @ 22:40
    Eduardo Rivas
    0

    Umbraco 5: Render image from Upload in child

    I'm cycling through the children of the current page, and I want to render an image (type upload with alias myImage), and some text (type textarea with alias myText). Currently I have this:

    @foreach (var item in DynamicModel.Children)
      {
          <div class="product">
          <img src="@item.myImage" alt="Photo of @item.Name" />
          <h2>@item.Name</h2>
          <p>@item.myText</p>
          </div>
      }

    But when I visit the page, the image is not rendered and the alt attribute is shown. All other fields work as expected. I'm using Umbraco 5 RTM build 5.0.310.16

  • Eduardo Rivas 17 posts 37 karma points
    Feb 22, 2012 @ 06:57
    Eduardo Rivas
    0

    Ok, so I've got this working:

    @inherits RenderViewPage
    @using System.Web.Mvc.Html
    @using Umbraco.Cms.Web;
    @using Umbraco.Framework;

    @foreach (var item in DynamicModel.Children)
      {
          <div class="product">
          <img src="@Umbraco.GetMediaUrl(item.Id, "myImage")" alt="Photo of @item.Name" />
          <h3>@item.Name</h3>
          <p>@item.myText</p>
          </div>
      }

    So for anyone wondering how to get an image from and Upload property, the sintax is @Umbraco.GetMediaUrl(page.Id, "propertyAlias")

    The problem is that now this is not working for every case. In a level 3 page I can get the image from the children at level 4. However, in another page at level 2, I cannot get images from children in level 3. I get this error (my translation) "Not valid URL: cannot analyze hostname" System.UriFormatException appears in the details. Any ideas?

  • Eduardo Rivas 17 posts 37 karma points
    Feb 24, 2012 @ 08:06
    Eduardo Rivas
    0

    Apparently, the error occurs if the upload field is left empty or a previous image is deleted. @Umbraco.GetMediaUrl should know how to handle this situation, because the page won't compile as it is right now. The workaround is and if statement or similar that checks if the Upload field has something in it before callind @Umbraco.GetMediaUrl. Any ideas on what kind of statement could I use?

  • Fredrik Liljemark 52 posts 74 karma points
    Apr 12, 2012 @ 16:56
    Fredrik Liljemark
    0

    I have the same issue as Eduardos last post...

    Any ideas?

Please Sign in or register to post replies

Write your reply to:

Draft