Copied to clipboard

Flag this post as spam?

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


  • Rylan 69 posts 253 karma points
    Sep 13, 2017 @ 15:39
    Rylan
    0

    Foreach Loop, then a content pickers page content? (7.4.3)

    I have a for each loop that pulls each recipient. Each recipient has a content picker that selects a elf (separate doc type and page). From the sponsor I want to select two fields, first name and last name

    Here's my code, I can't seem to get this one working.

    @foreach (var item in CurrentPage.Recipient.Where("UmbracoNaviHide == false"))
                            {   <td>
                                    @item.Id
                                </td>
                                <td>
                                    @if(item.HasValue("elf")){
                                        var elfContent = item.Content.GetPropertyValue<IEnumerable<IPublishedContent>>("elf");
                                        var elfBtn = Umbraco.TypedContent(item.GetPropertyValue<int>("elf"));
                                        foreach (var subItem in elfContent){
                                            <a href="@elfBtn.Url">
                                                @subItem.firstName @subItem.lastName        
                                            </a>
                                        }
                                    }
                                    else{
                                        <span>-</span>
                                    }
                                </td>
                            }
    
  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 13, 2017 @ 16:45
    Alex Skrypnyk
    0

    Hi Rylan

    This code works only with Property Value Converters enabled, do you have it? Property Value Converters are part of the core only since 7.6 version.

    Thanks,

    Alex

  • Rylan 69 posts 253 karma points
    Sep 13, 2017 @ 20:40
    Rylan
    0

    I'm not using 7.6 properties im running 7.4

    I tried using multiple ways to make this work including 7.6 code and couldn't figure it out.

  • Rylan 69 posts 253 karma points
    Sep 18, 2017 @ 17:01
    Rylan
    0

    anyone?

    Also tried this:

    @foreach (var item in CurrentPage.Recipient.Where("UmbracoNaviHide == false"))
                            {
                                if(item.HasValue("elf")){
                                    var elfBtn = Umbraco.TypedContent(item.GetPropertyValue<int>("elf"));
                                    <a href="@elfBtn.Url">
                                        @elfBtn.firstName @elfBtn.lastName
                                        </a>
                                }
                                else{
                                    <span>-</span>
                                }
                            }
    

    enter image description here

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 18, 2017 @ 21:57
    Alex Skrypnyk
    0

    Hi Rylan

    Try this code:

    @foreach (var item in Umbraco.AssignedContentItem.Children)
    {
        if(item.HasValue("elf")){
            var elfBtn = Umbraco.TypedContent(item.GetPropertyValue<int>("elf"));
            <a href="@elfBtn.Url">
                @elfBtn.GetPropertyValue("firstName") @elfBtn.GetPropertyValue("lastName")
            </a>
        }
        else{
            <span>-</span>
        }
    }
    

    Do not mix dynamics with strongly typed models.

    Thanks,

    Alex

  • Rylan 69 posts 253 karma points
    Sep 18, 2017 @ 22:56
    Rylan
    0

    Thanks for the response Alex. But it didn't seem to work. It actually decided to break the loop. This is before I even get to the content picker in the loop.

    enter image description here

    If I leave it with (below):

                                @foreach (var item in CurrentPage.Recipient.Where("UmbracoNaviHide == false"))
    

    I get this error:

    enter image description here

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 19, 2017 @ 07:04
    Alex Skrypnyk
    0

    Rylan, this code " @foreach (var item in CurrentPage.Recipient.Where("UmbracoNaviHide == false"))" and code that gives an error, can't work

    Please use strongly typed objects.

    CurrentPage is a dynamic object, try to avoid it.

    Thanks,

    Alex

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 19, 2017 @ 08:00
    Alex Skrypnyk
    0

    This code isn't working because of this line:

    @foreach (var item in Umbraco.AssignedContentItem.Children)
    

    Rylan, try this loop instead:

    @foreach (var item in Umbraco.AssignedContentItem.GetPropertyValue

    Thanks,

    Alex

  • Rylan 69 posts 253 karma points
    Sep 19, 2017 @ 17:17
    Rylan
    0

    It works fine normally when I don't try to pull specific fields from the content pickers page.

    Normally the .Url .Name will work just fine.

    I added

    @foreach (var item in Umbraco.AssignedContentItem.GetPropertyValue("recipient"))
    

    and get this error

    enter image description here

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 19, 2017 @ 18:46
    Alex Skrypnyk
    0

    Hi Rylan

    Did you solve the issue?

    Thanks,

    Alex

  • Rylan 69 posts 253 karma points
    Sep 19, 2017 @ 19:06
    Rylan
    0

    Not yet Alex, that didn't make it work lol thats what error I get above.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 19, 2017 @ 19:11
    Alex Skrypnyk
    0

    This code is working for me:

    @foreach (var item in Umbraco.AssignedContentItem.GetPropertyValue<IEnumerable<IPublishedContent>>("recipient").Where(x => x.IsVisible()))
    {
        <td>
            @item.Id
        </td>
        <td>
            @if (item.HasValue("elf"))
            {
                var elfContent = item.GetPropertyValue<IEnumerable<IPublishedContent>>("elf");
                var elfBtn = Umbraco.TypedContent(item.GetPropertyValue<int>("elf"));
                foreach (var subItem in elfContent)
                {
                    <a href="@elfBtn.Url">
                        @subItem.GetPropertyValue("firstName") @subItem.GetPropertyValue("lastName")
                    </a>
                }
            }
            else
            {
                <span>-</span>
            }
        </td>
    }
    
  • Rylan 69 posts 253 karma points
    Sep 19, 2017 @ 19:22
    Rylan
    0

    Thanks Alex, I tried that code outside of my current loop and it still isn't working. I get a "Value cannot be null."

    Do you think I'm not calling the right Models?

    at the top of the page I have:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage<ContentModels.RecipientList>
    
    @using ContentModels = Umbraco.Web.PublishedContentModels;
    
  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 19, 2017 @ 19:23
    Alex Skrypnyk
    0

    What line causes the problem?

    Maybe problem with this line - Umbraco.AssignedContentItem.GetPropertyValue<>

    What type of property is "recipient"?

    Thanks,

    Alex

  • Rylan 69 posts 253 karma points
    Sep 19, 2017 @ 19:26
    Rylan
    0

    Yes, it is a problem with that line.

    Recipient is a document type.

    Thanks for all the help so far, hoping to get this workign soon Lol.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 19, 2017 @ 19:27
    Alex Skrypnyk
    0

    Where are "Recipient" documents placed?

    Are they under the current page?

  • Rylan 69 posts 253 karma points
    Sep 19, 2017 @ 19:30
    Rylan
    0

    Yes the current page would "RecipientList" which only allows the doctype 'recipient' to be displayed on the list. If there's a way I can screenshare with you that could work.

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 19, 2017 @ 19:31
    Alex Skrypnyk
    0

    Then iterate over child nodes like here:

    @foreach (var item in Umbraco.AssignedContentItem.Children.Where(x => x.IsVisible()))
    {
        <td>
            @item.Id
        </td>
        <td>
            @if (item.HasValue("elf"))
            {
                var elfContent = item.GetPropertyValue<IEnumerable<IPublishedContent>>("elf");
                var elfBtn = Umbraco.TypedContent(item.GetPropertyValue<int>("elf"));
                foreach (var subItem in elfContent)
                {
                    <a href="@elfBtn.Url">
                        @subItem.GetPropertyValue("firstName") @subItem.GetPropertyValue("lastName")
                    </a>
                }
            }
            else
            {
                <span>-</span>
            }
        </td>
    }
    
  • Rylan 69 posts 253 karma points
    Sep 19, 2017 @ 19:34
    Rylan
    0

    Getting closer I think.

    enter image description here

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 19, 2017 @ 19:41
    Alex Skrypnyk
    100

    New variant of code:

    @foreach (var item in Umbraco.AssignedContentItem.Children.Where(x => x.IsVisible()))
    {
        <td>
            @item.Id
        </td>
        <td>
            @if (item.HasValue("elf"))
            {
                var elfContent = item.GetPropertyValue<string>("elf").Split(',').Select(x => int.Parse(x));
    
                foreach (var subItemId in elfContent)
                {
                    var elfContentNode = Umbraco.TypedContent(subItemId);
    
                    <a href="@elfContentNode.Url">
                        @elfContentNode.GetPropertyValue("firstName") @elfContentNode.GetPropertyValue("lastName")
                    </a>
                }
            }
            else
            {
                <span>-</span>
            }
        </td>
    }
    
  • Rylan 69 posts 253 karma points
    Sep 19, 2017 @ 19:42
    Rylan
    0

    Holy crap! You did it!

    Yay!

    Thanks Alex for all the help :)

  • Alex Skrypnyk 6182 posts 24284 karma points MVP 8x admin c-trib
    Sep 19, 2017 @ 19:43
    Alex Skrypnyk
    0

    Rylan, You are welcome!!!!

    Really help to achieve it.

    Alex

  • 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