Copied to clipboard

Flag this post as spam?

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


  • Matt 353 posts 825 karma points
    Sep 18, 2019 @ 11:37
    Matt
    0

    Hi,

    I've added a alt property to my image type and want to render this out.

    I have the following line of code;

    string mainImageAltText = carouselId.GetPropertyValue<string>("umbracoAlt");
    

    Which I then render with

    @mainImageAltText
    

    Now I seem to be getting this error and not sure why?

    CS1061: 'string' does not contain a definition for 'GetPropertyValue' and no accessible extension method 'GetPropertyValue' accepting a first argument of type 'string' could be found (are you missing a using directive or an assembly reference?)

    Thanks in advance,

    Matt

  • Niels Hartvig 1951 posts 2391 karma points c-trib
    Sep 18, 2019 @ 11:46
    Niels Hartvig
    0

    Hi Matt!

    Could it be that "carouselId" is a string containing an id and not the actual representation of the image? Then you'd need to retrieve the image first using the id: https://our.umbraco.com/Documentation/Getting-Started/Design/Rendering-Media/

  • Matt 353 posts 825 karma points
    Sep 18, 2019 @ 12:08
    Matt
    0

    Hi Neils,

    I'm afraid I'm a little lost now!

    here is my code;

        @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    
    @{
        var selection = Umbraco.Content(Guid.Parse("86acfb75-8edc-4d6d-933d-d8c8f555c57a"))
        .ChildrenOfType("newsItem")
        .Where(x => x.IsVisible())
        .OrderByDescending(x => x.CreateDate)
        .Take(4);
    
        string mainImageAltText = newsImage.GetPropertyValue<string>("umbracoAlt");
    }
    
    
      <div class="latest-news">
        <div class="div-block-4">
          <h3 class="latest-news-heading">Latest news</h3>
        </div>
        <div class="news-container">
          <div class="w-row">
    
          @foreach (var item in selection){
              var newsImage = item.Value<IPublishedContent>("newsImage");
        <div class="w-col w-col-3">
          <div class="news-item-wrapper">
                @if (newsImage != null)
                {
                 <img src="@newsImage.Url" alt="@mainImageAltText" width="300">
                 }
    
            <div class="news-date">
              <div class="news-date-text">@item.CreateDate.ToString("dd MMMM yyyy")</div>
            </div>
             <a href="@item.Url" class="link-block-4 w-inline-block">
                  <div class="paragraph">@item.Value("newsTitle")</div>
             </a>
          </div>
        </div>
    }
    
          </div>
        </div>
      </div>
    

    So on this line;

    string mainImageAltText = newsImage.GetPropertyValue<string>("umbracoAlt");
    

    "newsImage" is what my mediaPicker is called.

    But I get

    Compiler Error Message: CS0103: The name 'newsImage' does not exist in the current context

  • Jonathan Distenfeld 105 posts 618 karma points
    Sep 18, 2019 @ 12:27
    Jonathan Distenfeld
    1

    Hi,

    the problem is that you are using newImage before you define it. Your image has the altText, so you can only get the altText if you have the image.

    Try moving this line:

    string mainImageAltText = newsImage.GetPropertyValue<string>("umbracoAlt");
    

    to a place after you defined your newsImage:

          @foreach (var item in selection){
              var newsImage = item.Value<IPublishedContent>("newsImage");
              string mainImageAltText = newsImage.GetPropertyValue<string>("umbracoAlt");
    

    ~ Jonathan

  • Matt 353 posts 825 karma points
    Sep 18, 2019 @ 12:50
    Matt
    0

    Hi,

    Thanks I've moved that now I'm getting the following;

    CS1061: 'IPublishedContent' does not contain a definition for 'GetPropertyValue' and no accessible extension method 'GetPropertyValue' accepting a first argument of type 'IPublishedContent' could be found (are you missing a using directive or an assembly reference?)

    I'm using umbraco 8

    Thanks

  • Jonathan Distenfeld 105 posts 618 karma points
    Sep 18, 2019 @ 13:08
    Jonathan Distenfeld
    0

    Hi Matt,

    sorry, in U8 you use:

    string mainImageAltText = newsImage.Value<string>("umbracoAlt");
    

    ~ Jonathan

  • Nik 1593 posts 7151 karma points MVP 6x c-trib
    Sep 18, 2019 @ 13:20
    Nik
    0
    @inherits Umbraco.Web.Mvc.UmbracoViewPage
    
    @{
        var selection = Umbraco.Content(Guid.Parse("86acfb75-8edc-4d6d-933d-d8c8f555c57a"))
            .ChildrenOfType("newsItem")
            .Where(x => x.IsVisible())
            .OrderByDescending(x => x.CreateDate)
            .Take(4);
    }
    
    <div class="latest-news">
        <div class="div-block-4">
            <h3 class="latest-news-heading">Latest news</h3>
        </div>
        <div class="news-container">
            <div class="w-row">
    
                @foreach (var item in selection){
                    var newsImage = item.Value<IPublishedContent>("newsImage");
                    <div class="w-col w-col-3">
                        <div class="news-item-wrapper">
                            @if (newsImage != null)
                            {
                                <img src="@newsImage.Url" alt="@newsImage.Value<string>("umbracoAlt")" width="300">
                            }
    
                            <div class="news-date">
                                <div class="news-date-text">@item.CreateDate.ToString("dd MMMM yyyy")</div>
                            </div>
                            <a href="@item.Url" class="link-block-4 w-inline-block">
                                <div class="paragraph">@item.Value("newsTitle")</div>
                            </a>
                    </div>
                </div>
            }
        </div>
    </div>
    

    Try something like that.

    What I've done is move where you are getting your alt description to where you need it.

    Thanks

    Nik

  • Jonathan Distenfeld 105 posts 618 karma points
    Sep 18, 2019 @ 13:36
    Jonathan Distenfeld
    0

    Hi Nik,

    thanks for your contribution.

    Unfortunatly this codeline:

    <img src="@newsImage.Url" alt="@newsImage.Value<string>("umbracoAlt")" width="300">
    

    won't work, because you cant use generic types in HTML like this.

    If you wan't to have type safety, the easiest way is to define an extra variable. If not you could use:

    <img src="@newsImage.Url" alt="@newsImage.Value("umbracoAlt")" width="300">
    

    ~ Jonathan

  • Paul Wright (suedeapple) 277 posts 704 karma points
    Sep 18, 2019 @ 15:28
    Paul Wright (suedeapple)
    0
    <img src="@newsImage.Url" alt="@(newsImage.Value<string>("umbracoAlt"))" width="300">
    

    Seems overly verbose to have an alternative "ALT" value, when newsImage.Name would probably suffice :-)

Please Sign in or register to post replies

Write your reply to:

Draft