Copied to clipboard

Flag this post as spam?

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


  • Craig P 14 posts 95 karma points
    Oct 23, 2023 @ 18:53
    Craig P
    0

    Accessing a BlockListItem directly like an array?

    I have a block list with characterName, characterPhoto, and characterDescription for example... I've restricted it in the blocklist so you have to create exactly 4 items.

    In the HTML template, the 4 characters are dotted around the page, not structured in a list or anything.

    I'd like to access my items in the HTML directly like an array. Something like pageCharacter[0].characterName and further down the page use pageCharacter[1].characterName

    I cannot use [] on the BlockListItem directly so how do I do this? The only way I know is to cycle through it using a foreach or something similar but this won't work in my case. Is there any way to do this?

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Oct 24, 2023 @ 05:05
    Chriztian Steinmeier
    0

    Hi Craig,

    A crude way would be to use .Skip(x) and .Take(1) to get at your items, but you are probably also able to convert the BlockListModel to an array by using a .ToArray<T>() or similar method, as it is derived from an IList (AFAIK).

    Hope this helps,

    /Chriztian

  • Craig P 14 posts 95 karma points
    Oct 24, 2023 @ 13:52
    Craig P
    0

    Thank you,

    I have used .ToList() and I can get string content from it fine using pageCharacter[0].Content.Value("characterName") but I can't work out how to get the URL from an image object this way?

    I have tried

    pageCharacter[0].Content.Value("characterImage").Url
    
    and pageCharacter[0].Content.Value<IPublishedContent>("characterImage").Url
    

    For example and both produce an error? Any pointers would be appreciated?

Please Sign in or register to post replies

Write your reply to:

Draft