Copied to clipboard

Flag this post as spam?

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


  • Craig100 1136 posts 2523 karma points c-trib
    Feb 14, 2015 @ 16:23
    Craig100
    0

    Get image from a child node MultiMedia Picker

    V7.2.1

    Site
    ---Product List
    ------Product 1
    ---------Sku 1
    ---------Sku 2
    ------Product 2
    ---------Sku 1
    ---------Sku 2

    I have the following code which is attempting to display the first image in the gallery in each Product's Sku page on the Product List page:-

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @{
        var productList = CurrentPage.Site().FirstChild("ProductList").Children("ProductItem").Where("Visible");
    
        foreach(var product in productList) {
            var skuList = product.Children.Where("Visible && DocumentTypeAlias == @0","Sku");
            var initialSku = skuList.First().sku;
        <section class="productpage-section">          
            <div class="row features-boxes product-border">
                <div class="col-l-3 col-m-3 col-s-3">
                    <div class="productimg-lft ortac">
    
                        @foreach (var thisSku in skuList) {
                             if (thisSku.HasValue("productImages")) {
                                //var imageList = thisSku.GetPropertyValue("productImages");
    
                                //var imageList = thisSku.productImages.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
    
                                var imageList = thisSku.GetPropertyValue<string>("productImages").Split(new string[] { "," },StringSplitOptions.RemoveEmptyEntries);                          
    
                                var productImage = Umbraco.Media(imageList[0]);
                                <img src="@productImage.GetResponsiveCropUrl("ProductUSP")" alt="@productImage.Name">
                            }
                        }
                    </div>
                </div>
            </div>
        </section>
    }

    In the code above there are three attempts at getting the reference to the MMP. The first one:

    var imageList = thisSku.GetPropertyValue("productImages");

    Results in an error saying  "The value of parameter 'id' must be either a string or an integer". Fair enough, I obviously need to provide an ID to the Umbraco.Media(int) call.

    The second one, which is an attempt to get an array of ID's from the MMP:

    var imageList = thisSku.productImages.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);

    Results in an error saying "Umbraco.Web.Models.DynamicPublishedContentList' does not contain a definition for 'Split'" which is a bit worrying on the face it, given other forum posts alluding to .Any being missed on previous builds of V7.x.

    The third one:

    var imageList = thisSku.GetPropertyValue("productImages").Split(new string[] { "," },StringSplitOptions.RemoveEmptyEntries);

    Fails siliently. The page is displayed with empty img tags.

    I'm at a loss as to what to try next. Any advice would be appreciated.

    Craig

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Feb 14, 2015 @ 17:02
    Jeavon Leopold
    0

    Hi Craig,

    Do you have my value converters package installed?

    Jeavon

  • Craig100 1136 posts 2523 karma points c-trib
    Feb 14, 2015 @ 17:41
    Craig100
    0

    HI Jeavon

    I have the Umbraco Core Property Value Converters but I don't think I'm actually using it as I don't know how and finding documentation in here is a nightmare.

    I read somewhere that these value converters can cause issues with existing written code? I'm on a very tight deadline so don't want to risk delays, hence nervousness. However, if what I read was incorrect, I'm all ears ;)

    Craig

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Feb 14, 2015 @ 18:16
    Jeavon Leopold
    100

    Ok, if it's installed then it's converting, so there's no need to split.

    Try simply this:

    var productImage = thisSku.productImages.First();
    
  • Craig100 1136 posts 2523 karma points c-trib
    Feb 14, 2015 @ 18:29
    Craig100
    0

    That worked!  So I'd been trying too hard. But, as with a lot of Umbraco coding, how do you discover this stuff? I waste hours and hours :(

    Thanks Jeavon.

  • Jeavon Leopold 3074 posts 13632 karma points MVP 11x admin c-trib
    Feb 14, 2015 @ 18:34
    Jeavon Leopold
    0

    In this case it is because when you have the converters, they are doing the work to make it simpler for you. I have tried to provide detailed documentation and samples for them here, have you seen it before?

  • Craig100 1136 posts 2523 karma points c-trib
    Feb 14, 2015 @ 18:47
    Craig100
    0

    Yes, I vaguely remember seeing that top level page and thinking it's actually making it harder and that if I used it, it could cause me even more confusion when looking at various code examples on the web. I thought I'd remove it at the time, but I didn't so I guess I'm stuck with it now. I'll have to have a more in depth read.

    Thanks again.

    Craig

Please Sign in or register to post replies

Write your reply to:

Draft