Copied to clipboard

Flag this post as spam?

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


  • Matt 358 posts 841 karma points
    Jan 15, 2024 @ 10:56
    Matt
    0

    URL picker in block list umbraco 12

    Hi all,

    I'm trying to see if there are any code samples of how to use url picker inside of a block list.

    I'm trying this but no joy.

    var links3 = Model.Value<IEnumerable<BlockListItem>>("resourceLink");
        if (links3.Any())
    {
                @foreach (var link in links3)
            {
            <a href="@link.Url" target="@link.Target" class="button-2 _2 w-button">Click here for useful Numbers</a>
                                        }
    }
    

    }

  • Asembli 85 posts 259 karma points
    Jan 15, 2024 @ 21:44
    Asembli
    0

    Hi, 'Link' is of type Link, not BlockListItem. In the loop, you have to extract the Link from each BlockListItem

    IEnumerable<BlockListItem> blockListItems = something?.Value<IEnumerable<BlockListItem>>("something");
    if (blockListItems != null && blockListItems.Count() > 0)
    {
        foreach (BlockListItem blockListItem in blockListItems)
        {
            Link link = blockListItem.Content.Value<Link>("link");
            if(link != null)
            {
                <li class="p-1">
                    <a href="@link.Url" target="@link.Target" class="btn btn-primary">@link.Name</a>
                </li>
            }
        }
    }
    

    Regards

  • Matt 358 posts 841 karma points
    Jan 16, 2024 @ 07:12
    Matt
    0

    Hi Thanks,

    I did the following but I'm not getting any data for my link button, I also get no error messages the page loads fine just no link.

    Below is my code

      <div class="new-section">
        <div class="grid-wrapper">
          <div class="resources-col w-row">
    
    
    
              @{
        var variants = Model.Value<IEnumerable<BlockListItem>>("resourcesList").Select(x => x.Content);
        foreach (var variant in variants)
        {
    
                           <div class="w-col w-col-4">
              <div class="resources-div-wrapper">
                <h1 class="resources-heading">@variant.Value("resourceName")</h1>
                <p>@variant.Value("resourceDescription")</p>
    
     @{
         IEnumerable<BlockListItem> blockListItems = Model.Value<IEnumerable<BlockListItem>>("resourcesList");
            if (blockListItems != null && blockListItems.Count() > 0)
        {
                    foreach (BlockListItem blockListItem in blockListItems)
                {
                 Link link = blockListItem.Content.Value<Link>("resourceLink");
            if(link != null)
            {
                <li class="p-1">
                    <a href="@link.Url" target="@link.Target" class="btn btn-primary">@link.Name</a>
                </li>
            }
        }
    }
    }
    
              </div>
            </div>
    
        }
    }
          </div>
        </div>
      </div>
    

    resourcesList is my block list name resourceLink is my URL picker.

    Any help is appreciated.

    Thanks

  • Danine Noble 82 posts 368 karma points
    Jan 17, 2024 @ 18:11
    Danine Noble
    0

    I'm not sure why you are doing a double foreach through the same list. (or is 2nd one a typo & meant to be a different field name?)

    Unless there's a nested blocklist field in there that's not reflected in this structure, I am assuming 'resourceLink' is singular and a direct field of 'resourcesList'. If it isn't, then that's probably where the issue is.

    Could you use the existing variant iteration to get link field data like the other 2 fields above it? I haven't extracted data from a block list like this before so not totally sure, but if 'resourceLink' is just a field on the same item like the 'resourceDescription' and 'resourceName' are, then have you tried following those as an example? Something like:

    ...
    <p>@variant.Value("resourceDescription")</p>
    @{
        Link link = variant.Value("resourceLink");
        if (!string.IsNullOrWhiteSpace(link?.Url)) 
        {
            ...
        }
    }
    

    What have you tried debugging? Have you checked for which stage it's failing at (if checks or empty list)? When you say 'no link' which markup isn't rendering? The entire list-item block or just the @link variables are empty?


    Random note, note sure if it's just cause you cleaned this up for ease of reading here, but it looks like your li-tags are missing their parent element ;3 (ul/ol)

Please Sign in or register to post replies

Write your reply to:

Draft