Copied to clipboard

Flag this post as spam?

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


  • Brian 24 posts 44 karma points
    Oct 17, 2014 @ 15:50
    Brian
    0

    Looping through multinode treepicker and media picker and creating images

    Hi everyone. 
    I'm having an issue with looping through images on a page and getting it to display all the images. I have 4 images in a media picker, in a multinode treepicker. I think my loop may be bad? maybe I'm calling something incorrectly? Whichever it is, I am not sure.

                    @{

                    foreach (var temp in Umbraco.Content(clientCollection))

                    {

                    <img src="@Umbraco.Media(temp.clientLogo).Url" alt="@Umbraco.Media(temp.clientLogo).UrlName" />

               }

            } 

    Where clientCollection is var clientCollection = clientList; and clientList is 
    var clientList = CurrentPage.clientList.ToString().Split(new string[] { "," }, 

    In Umbraco, if i remove all but one of the items in clientList, so I only have one node of the multinode treepicker, it works just fine and prints out exactly how I want it to. As soon as I add the other clients, I get this error thrown at the start of that loop. Any suggestions?

     
    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: The call is ambiguous between the following methods or properties: 'Umbraco.Web.UmbracoHelper.Media(params int[])' and 'Umbraco.Web.UmbracoHelper.Media(params string[])'


  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 17, 2014 @ 15:54
    Dave Woestenborghs
    0

    Try this code :

    @{
    
                    foreach (var id in clientCollection)
    
                    {
    var temp = Umbraco.Content(id); 
    
                    <img src="@Umbraco.Media(temp.clientLogo).Url" alt="@Umbraco.Media(temp.clientLogo).UrlName" />
    
               }
    
            } 
    

     

    clientCollection is a list of id's. And Umbraco.Content only can take one id.

     

    Dave

  • Brian 24 posts 44 karma points
    Oct 17, 2014 @ 15:59
    Brian
    0

    Ah, I understand. I'm still getting the same issue, but I was thinking it might have something to do with that. 

    The error is now on the var temp = Umbraco.Content(id); if that helps. 

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 17, 2014 @ 16:12
    Dave Woestenborghs
    0

    Is it the same error or something different ?

  • Brian 24 posts 44 karma points
    Oct 17, 2014 @ 16:13
    Brian
    0

    Same one

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 17, 2014 @ 16:18
    Dave Woestenborghs
    0

    Can you try to out put just the id's to see what that gives you :

    @{
        foreach (var id in clientCollection)
        {
            <p>@id</p>
        }
    } 

    Dave

  • Brian 24 posts 44 karma points
    Oct 17, 2014 @ 16:22
    Brian
    0

    Man, this just confuses me even more. I was hoping it wasn't working, but when I tried that, I get all the id's perfectly. 

    1122

    1130

    1134

    1135

     

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 17, 2014 @ 16:24
    Dave Woestenborghs
    0

    Ah..I found it. Umbraco.Content takes a int parameter if I am correct. This "should" work.

    @{
        foreach (var id in clientCollection)
        {
            var temp = Umbraco.Content(int.Parse(id)); 
            if(temp != null) 
            {
                <img src="@Umbraco.Media(temp.clientLogo).Url" alt="@Umbraco.Media(temp.clientLogo).UrlName" />
            }
        }
    } 

    Dave

  • Brian 24 posts 44 karma points
    Oct 17, 2014 @ 16:27
    Brian
    0

    It's much happier, and getting passed the temp now, but the same issue remains on the <img  tag. 
    As a test, I swapped out the temp.clientLogo with id.clientLogo, just to see what it would print out, and it printed that the string does not contain a method called clientLogo, which while makes sense, is a little different.  

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 17, 2014 @ 16:30
    Dave Woestenborghs
    0

    Try this :

    var temp = Umbraco.TypedContent(int.Parse(id));

     

    And then you should try temp.GetPropertValue<int>("clientLogo"). 

    This way you use the strongly typed IPublishedContent model instead of the dynamic model which is hard to debug.

    Dave

  • Brian 24 posts 44 karma points
    Oct 17, 2014 @ 16:40
    Brian
    0

    Hm... Tested the temp.GetPropertValue<int>("clientLogo") And I got 

    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for 'GetPropertValue'

    eI made sure to set the var as TypedContent as well. When swapping out the temp.clientLogo tags for the temp.GetPropertyValue... in the image tag, I get the same error. When I leave it as it was, as temp.clientLogo, with the TypedContent in place, I get 


    Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'Umbraco.Web.Models.PublishedContentBase' does not contain a definition for 'clientLogo'

     I'm not sure what's going wrong at this point.

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 17, 2014 @ 16:41
    Dave Woestenborghs
    0

    Can you post the entire code of your view ?

    Dave

  • Brian 24 posts 44 karma points
    Oct 17, 2014 @ 16:49
    Brian
    0

    As of now, the view is incredibly long, so I removed most of the </divs> and such, 

    @inherits UmbracoTemplatePage

    @using umbraco.MacroEngines

    @{

    Layout = "Base.cshtml";

    var homePage = CurrentPage.AncestorsOrSelf(1).First();

    var clientList = CurrentPage.clientList.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

    var clientCollection = clientList;

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>

    <script type="text/javascript" src="/js/jquery.bxslider.min.js"></script>

    <link href="/css/jquery.bxslider.css" rel="stylesheet" />

     



     (useless working divs here, no code at all)

            <div class="small-12 large-10 columns logos">

                <ul class="small-block-grid-2 medium-block-grid-4 large-block-grid-4">

                    @{

                    foreach (var id in clientCollection)

                    {

                    var temp = Umbraco.TypedContent(int.Parse(id));

                    

                    if(temp != null) 

                    {

                    <img src="@Umbraco.Media(temp.clientLogo).Url" alt="@Umbraco.Media(temp.clientLogo).UrlName" />

                }

            }

        }

    </ul>

    </div> 

  • Brian 24 posts 44 karma points
    Oct 17, 2014 @ 17:30
    Brian
    0

    Still stuck on this  =\ Does anyone have any ideas?

  • Charles Afford 1163 posts 1709 karma points
    Oct 18, 2014 @ 12:52
    Charles Afford
    0

    Sure what is the problem with it :)

  • Charles Afford 1163 posts 1709 karma points
    Oct 18, 2014 @ 13:00
    Charles Afford
    0

    So if you have a property with media ids

    You can use the MediaService to get a object of type IMedia

    MediaService mediaService = ApplicationContext.Current.Services.MediaService; var media = mediaService.GetMediaById(your id)

    if(media != null) { var url = media.GetValue("umbracoFile"); }

    I have not got visual studio open for intellisense but it should be something like that. :).

    Let us know if that helps :).

    Charlie

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 19, 2014 @ 13:10
    Dave Woestenborghs
    0

    Hi Charles,

    The media service is not for use in you views as this queries the database and can become a performance bottle neck.

    Dave

     

  • Dave Woestenborghs 3325 posts 11170 karma points MVP 5x admin c-trib
    Oct 19, 2014 @ 13:38
    Dave Woestenborghs
    0

    Hi Brian,

    This code works for me assuming that the clientList property is a mediapicker.

    @{
        string clientList = Model.Content.GetPropertyValue<string>("clientList"); 
    
        List<int> ids = new List<int>();
    
        if(!string.IsNullOrEmpty(clientList)) 
        {
            foreach(string id in clientList.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries)) 
            {
                ids.Add(int.Parse(id)); 
            }
        }
    }
    
    @if(ids.Any()) 
    {
        foreach(int id in ids)
        {
            IPublishedContent mediaItem = Umbraco.TypedMedia(id);
    
            if(mediaItem != null) 
            {
                 string imageSrc = mediaItem.GetPropertyValue<string>("umbracoFile");
                 <img src="@imageSrc" alt="@mediaItem.Name" />
            }
        }
    }

     

    Dave

  • Charles Afford 1163 posts 1709 karma points
    Oct 25, 2014 @ 18:24
    Charles Afford
    0

    Awesome thanks for that Dave.

    Re: Media Service.

    Thanks, Yea usually i would have a model being passed in to the view / partial with the media object already instanced or properties with URL ect ect :)

    What is the performance with Umbraco.TypedMedia?

    Charlie

  • 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