Copied to clipboard

Flag this post as spam?

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


  • Elin 45 posts 166 karma points
    Sep 11, 2017 @ 19:45
    Elin
    0

    Get every first, second and third Text Field in Nested Content, and place in columns

    Hi,

    How can I get the nested content text fields to be placed in different grid columns through razor code ? (see image below for clarification)

    Nested Content Setup to Grid Columns Layout

    Below is the code I setup so far but it's not working, I keep getting an error saying :

    Operator '%' cannot be applied to operands of type 'Umbraco.Web.PublishedContentModels.BigAndSmallTextFields' and 'int'

    and I perfectly understand what the error mean, I just can't find a good solution to my problem.

        @{
             var List = row.GetPropertyValue<IEnumerable<IPublishedContent>>("rowNestedContent");
             foreach (var item in List) {
                if (item % 3 == 0) {
                   <li>
                      <span class="bullet-text">@Umbraco.Field(item, "nestedBigText")
                         <span class="small-bullet-text">@Umbraco.Field(item, "nestedSmallText")</span>
                      </span>
                   </li>
                }
             }
         }
    

    Any help will be greatly appreciated, thanks.

  • Kevin Jump 2341 posts 14868 karma points MVP 8x c-trib
    Sep 12, 2017 @ 07:03
    Kevin Jump
    0

    Hi

    the quick way would be to change the foreach to a for loop ?

    for(int i = 0; i < item.Count; i++)
    {
        if (i % 3 == 0)
        {
            @Umbraco.Field(item[i], "nestedBigText")
        }
    }
    
  • Elin 45 posts 166 karma points
    Sep 12, 2017 @ 14:12
    Elin
    0

    Hi Kevin,

    Thank you replying, I took your advice and tried, it, I ended up with the below error:

    'umbraco.item' does not contain a definition for 'Count'

    Also, the foreach was specifying that for each item inside the List, to do something, but in your code, "item" hasn't been defined at all. I don't know if what I'm saying makes any sense to you or if I may be wrong.

  • Kevin Jump 2341 posts 14868 karma points MVP 8x c-trib
    Sep 12, 2017 @ 14:18
    Kevin Jump
    100

    Hi ,

    yeah Count works if your values are turned into a list (via .ToList()) but Count() should work without that.

    and yes it should be List not item. my bad. try ...

    for(int i = 0; i < List.Count; i++)
    {
        if (i % 3 == 0)
        {
            @Umbraco.Field(List[i], "nestedBigText")
        }
    }
    

    You might also want to change the Variable name from List to something else, as there is a .net List class and it might get confusing...

  • Elin 45 posts 166 karma points
    Sep 12, 2017 @ 14:26
    Elin
    0

    Ok, I changed the List to listItems, now I'm getting a new error

    Non-invocable member 'System.Collections.Generic.List

    For reference this is what I have so far

    @{
        var listItems = row.GetPropertyValue<IEnumerable<IPublishedContent>>("rowNestedContent");
        for (int i = 0; i < listItems.Count(); i++) {
            if (i % 3 == 0) {
                <li>
                    <span class="bullet-text">
                        @Umbraco.Field(listItems[i], "nestedBigText")
                        <span class="small-bullet-text">@Umbraco.Field(listItems[i], "nestedSmallText")</span>
                    </span>
                </li>
            }                                           
        }
    }
    

    *UPDATE*

    Your code actually worked, all I did was removing the parentheses () from the Count (Count() to Count)

    Thank a lot for helping with this Kevin, I really appreciated your help solving this.

Please Sign in or register to post replies

Write your reply to:

Draft