Copied to clipboard

Flag this post as spam?

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


  • Jimmy 19 posts 118 karma points
    Jun 29, 2024 @ 10:57
    Jimmy
    0

    Model.Value missing in BlockList

    Hi

    I'm using a BlockList/BlockGrid. I need to get a value from a property up the tree from where the block is being called.

    On normal pages I could do something like

    Model.Value....
    

    But when I do this in a block component Value doesn't exist.

    How do I retrieve a value from up the tree in a block?

    Thank you

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Jun 29, 2024 @ 12:34
    Huw Reddick
    1

    Is this component on the same node as the grid, or a different node.

    Blocks have content items which are block items.

    Maybe if you could explain exactly what you are attempting, someone can point you in the right direction

  • Jimmy 19 posts 118 karma points
    Jun 29, 2024 @ 13:13
    Jimmy
    0

    Hi Huw

    The block is under views-partial-components.

    I render it on a page a few levels deep using

    @Html.GetBlock.....
    

    The block displays the content but the value I need to return for example could be up 2 levels but if I reuse the block on a deeper node it could be 4 levels deep.

    For now and for the purpose of simplicity, we could say the value I need is at the root of the site.

    I have 2+ sites in the Umbraco so I can't use the first site the code finds should be the site I'm currently browsing.

    I hope this gives more clarity and information?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Jun 29, 2024 @ 14:01
    Huw Reddick
    0

    Hi Jimmy,

    Yes that explains a bit more. Are these nodes all descendants of the same root node, or do you have more than one root node.

    If the value is at the site root you can just do something like

    var siteroot = (IPublishedContent)Umbraco.ContentAtRoot().FirstOrDefault();
    

    then use siteroot.Value("aliasOfValue");

  • Jimmy 19 posts 118 karma points
    Jul 01, 2024 @ 06:42
    Jimmy
    0

    Hi Huw

    The code worked but on the different site it didn't. It looks like siteRoot gets the first in the list of site roots and not the one I'm browsing?

    Thank you

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Jul 01, 2024 @ 06:46
    Huw Reddick
    0

    Ok, just change the firstordefault so it has an expression that checks the for the alias of the root node you require.

  • Jimmy 19 posts 118 karma points
    Jul 01, 2024 @ 06:52
    Jimmy
    0

    I'm not sure I follow.

    If I change it to an alias name, like ws=> ws.Site.Alias would that not pick the same site each time the code is run?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Jul 01, 2024 @ 06:59
    Huw Reddick
    0

    Do you have multiple roots? could you perhaps post the layout of your nodes?

    You could try Model.Root() but not sure exactly what you are attempting.

  • Jimmy 19 posts 118 karma points
    Jul 01, 2024 @ 07:05
    Jimmy
    0

    My site structure is

    Site A
     --page A
    Site B
     --page B
    

    The same structure is followed throughout.

    Site ** is where the properly is with a value I need for all sites. When site A,B,C,D load I want the value from that sites parent node.

  • Jimmy 19 posts 118 karma points
    Jul 01, 2024 @ 07:15
    Jimmy
    0

    I think I may have it. However be great if you could confirm if this is the correct approach just in case this causes other weird problems during the line. I used

    var root = Umbraco.AssignedContentItem.Parent
    

    Which seems to be doing what I need but if that's the correct approach I'm not so sure?

  • Huw Reddick 1929 posts 6717 karma points MVP 2x c-trib
    Jul 01, 2024 @ 07:19
    Huw Reddick
    100

    That will work as long as your page is only one level deep. But will fail if you have say

    Site A
     --page A
     ----SubPage
    

    So you may be better using something like

    Umbraco.AssignedContentItem.AncestorOrSelf(1)
    
Please Sign in or register to post replies

Write your reply to:

Draft