Copied to clipboard

Flag this post as spam?

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


  • Brett Fullam 119 posts 629 karma points
    May 30, 2017 @ 03:09
    Brett Fullam
    0

    Archetype -- Media Picker -- Dynamic loop functionl, but need help adding a conditional "if" statemtent to allow a null option

    OK ... so earlier I was having problems accessing-rendering images from a media picker inside of an Archetype. I managed to resolve that, but I need to add a conditional "if" statement that allows a null option in case a user chooses not to populate the media picker with images.

    Here's what I have so far ... it works so long as the media pickers have images placed inside of them. When one of them doesn't it throws an error.

    Here's what I have so far ...

    @{
        var typedMultiMediaPicker = fieldset.GetValue<IEnumerable<IPublishedContent>>("selectImage");
        foreach (var item in typedMultiMediaPicker)
        {
            <div><img  class="img-responsive multiSlides" src="@item.Url" /></div>
        }
    }
    

    The difficulty I'm having is how to write the conditional statement like this that directly references a property in an Archetype:

    if (var select = fieldset.GetValue<IPublishedContent>("selectImage"))
    
    or 
    
    if (var select = fieldset.GetValue<IPublishedContent>("selectImage") != null)
    

    Both of these throw an error. Any suggestions?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 30, 2017 @ 09:40
    Alex Skrypnyk
    1

    Hi Brett

    Try this code:

    var typedMultiMediaPicker = fieldset.GetValue<IEnumerable<IPublishedContent>>("selectImage");
    var multiMediaPicker = typedMultiMediaPicker as IList<IPublishedContent> ?? typedMultiMediaPicker.ToList();
    if (multiMediaPicker.Any())
    {
        foreach (var item in multiMediaPicker)
        {
            <div><img class="img-responsive multiSlides" src="@item.Url"/>
            </div>
        }
    }
    
  • Brett Fullam 119 posts 629 karma points
    May 30, 2017 @ 12:47
    Brett Fullam
    0

    Hi Alex,

    Thanks for jumping in on this ... unfortunately, this throws an error too. Check out the error message: enter image description here

    [ArgumentNullException: Value cannot be null. Parameter name: source]

    Here's the code from the entire partial view if that helps rule anything out:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    @using Archetype.Models;
    @using Archetype.Extensions;
    
    
    @{
        Layout = null;
    }
    
    @foreach(var fieldset in Model.Content.GetPropertyValue<ArchetypeModel>("tabsSlider"))
    {
    
    <div class="panel panel-default" id="@fieldset.GetValue("anchorID")" style=" border-radius: 0px; border: 0;  background-color: transparent;">
        <div class="susPanelMobile panel-heading @fieldset.GetValue("isOpen")" style="height: 36px; padding: 0; background-color: #@fieldset.GetValue("tabCor");">
        <h2 class="accordionTitle"><a class="accordion-toggle collapsed" data-toggle="collapse" data-parent="#accordion" href="#@fieldset.GetValue("nome")">
            <div class="accordionTitle" style="height: 100%; width: 88%; float:left;">@fieldset.GetValue("tabTitle")</div></a></h2>
        </div>
    
        <div id="@fieldset.GetValue("nome")" class="panel-collapse collapse" style=" background-color: transparent;">
          <div class="panel-body body-copy-grey" style="background-color: transparent;">
    
                @Html.Raw(@fieldset.GetValue<string>("tabTexto"))
    
    <div class="[email protected]("tabNumero")" style="margin-bottom: 50px">
    
    @{  var typedMultiMediaPicker = fieldset.GetValue<IEnumerable<IPublishedContent>>("selectImage");
        var multiMediaPicker = typedMultiMediaPicker as IList<IPublishedContent> ?? typedMultiMediaPicker.ToList();
        if (multiMediaPicker.Any())
        {
        foreach (var item in multiMediaPicker)
        {
            <div><img class="img-responsive multiSlides" src="@item.Url"/>
            </div>
        }
    }
    }
    
      </div>
    
    
    
    
          </div>
        </div>
      </div>
    
      }
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 30, 2017 @ 15:44
    Alex Skrypnyk
    0

    Hi Brett

    If you debug? typedMultiMediaPicker is null?

  • Brett Fullam 119 posts 629 karma points
    May 30, 2017 @ 18:04
    Brett Fullam
    0

    Hey Alex,

    I've been trying to debug this ... can't get miniprofiler to work.

    Any suggestions on that .... this is killing me ha ha ha ... the last 24hrs has been a real struggle.

    Just point me in the right direction to debug if you can ... I'd like to be an active participant in the solution (process) here.

    Thanks again. Your help is always appreciated.

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 30, 2017 @ 21:13
    Alex Skrypnyk
    100

    Hi Brett

    it looks like the problem is when you have the possibility to add 0, 1 or few images in one picker.

    If user adds one image - GetValue returns IPublishedContent

    If user adds few images - GetValue returns IEnumerable

    This code works for me:

                @{
                    if (fieldset.HasValue("selectImage"))
                    {
                        var typedMultiMediaPicker = fieldset.GetValue<IEnumerable<IPublishedContent>>("selectImage");
                        if (typedMultiMediaPicker != null)
                        {
                            foreach (var item in typedMultiMediaPicker)
                            {
                                <div>
                                    <img class="img-responsive multiSlides" src="@item.Url"/>
                                </div>
                            }
                        }
    
                        var mediaPicker = fieldset.GetValue<IPublishedContent>("selectImage");
                        if (mediaPicker != null)
                        {
                            <div>
                                <img class="img-responsive multiSlides" src="@mediaPicker.Url"/>
                            </div>
                        }
                    }
                }
    

    Hope it will work for you.

    Thanks,

    Alex

  • Brett Fullam 119 posts 629 karma points
    May 31, 2017 @ 12:59
    Brett Fullam
    1

    That did it. Works perfectly now ... adds slider images when present and no longer throws an error when there aren't any images present. PERFECT...!

    Thanks, Alex. Really appreciate your help (as always)!

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    May 31, 2017 @ 13:17
    Alex Skrypnyk
    0

    You are welcome, Brett, it's pleasure to help. Have a great day!

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft