Copied to clipboard

Flag this post as spam?

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


  • Ault Nathanielsz 87 posts 407 karma points c-trib
    Nov 02, 2022 @ 19:04
    Ault Nathanielsz
    0

    How to get Block Item Settings in a Partial View that is Typed to the Block Item Content?

    I loop through a block list like this (simplified, but you get the idea):

    IEnumerable<BlockListItem> areaBlocks = Model.Value<IEnumerable<BlockListItem>>("content")!;
     if (areaBlocks != null)
     {
          foreach (BlockListItem blockItem in areaBlocks)
           {
                  IPublishedElement blockContent = blockItem.Content;
                  string blockAlias = blockContent.ContentType.Alias;
                  @await Html.PartialAsync(blockAlias, blockContent)
             }
     }
    

    Each partial view is named to match the blockAlias.

    From within the partial view, I would like to access the settings for the blockItem. I am not sure how to do this.

    I can access the node that contains the blockItem with Umbraco.AssignedContentItem

    However, I cannot find the code that will allow me to get the block list item of which this element is the content. That should allow me to get the settings element. Any thoughts?

  • Matthew Care 12 posts 145 karma points c-trib
    Nov 02, 2022 @ 20:33
    Matthew Care
    101

    My usual setup is something like this, where you call a "base" block list partial view containing

    @model IEnumerable<Umbraco.Cms.Core.Models.Blocks.BlockListItem>
    
    @foreach (var item in Model)
    {
        var path = $"~/Views/Partials/BlockList/{item.Content.ContentType.Alias.ToFirstUpper()}.cshtml";
        <partial name="@path" model="item" />
    }
    

    and then in the partial view for each element you can start the file with (replacing TContent and TSettings with your content and settings models)

    @model Umbraco.Cms.Core.Models.Blocks.BlockListItem<TContent, TSettings>
    

    Which then enables you to access the content and settings with

    Model.Content
    Model.Settings
    
  • Ault Nathanielsz 87 posts 407 karma points c-trib
    Nov 07, 2022 @ 10:36
    Ault Nathanielsz
    0

    Thanks Matthew- that works great.

    One question: how do you get values from the Umbraco.AssignedContentItem (i.e. from the node that contains the block list)?

    Previously this was available as the model was an ipublishedelement.

  • Ault Nathanielsz 87 posts 407 karma points c-trib
    Nov 07, 2022 @ 11:52
    Ault Nathanielsz
    0

    Matthew had this reply to my last question:

    If you instead change from @model BlockListItem..... to @inherits UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockListItem<TDocument, TSettings>>

    Then you can access the current page with something like

    @UmbracoContext.PublishedRequest.PublishedContent

    NB: AssignedContentItem isn't always the requested page

  • Ault Nathanielsz 87 posts 407 karma points c-trib
    Nov 07, 2022 @ 11:53
    Ault Nathanielsz
    0

    Matthew said this (elsewhere):


    If you instead change from @model BlockListItem..... to @inherits UmbracoViewPage<Umbraco.Cms.Core.Models.Blocks.BlockListItem<TDocument, TSettings>>

    Then you can access the current page with something like

    @UmbracoContext.PublishedRequest.PublishedContent

    NB: AssignedContentItem isn't always the requested page


Please Sign in or register to post replies

Write your reply to:

Draft