Copied to clipboard

Flag this post as spam?

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


  • Matt Taylor 859 posts 2052 karma points
    Oct 03, 2020 @ 10:10
    Matt Taylor
    0

    Simple conversion of Block List content items to List

    I'm busy trying to future proof a my current website project by converting all my Nested Content properties to Block Lists.

    In the deep dive documentation this is the suggested way to get the content from the Block List Content Items.

    @foreach(var blockItem in Model.MyProperty) {
            var blockItemContent = (ContentModels.MyElementTypeAlias)blockItem.Content;
            <h2>@blockItemContent.MyBlockProperty</h2>
     }
    

    With Nested Content I could easily convert the property values into an IEnumerable list of ContentModels.MyElementTypeAlias.

    If all I'm interested in is the Block List Item Content properties from each item in the Block List Model then is there a smart way to extract those straight into an IEnumerable list of ContentModels.MyElementTypeAlias?

    Thanks,

    Matt

  • Matt Taylor 859 posts 2052 karma points
    Oct 03, 2020 @ 10:53
    Matt Taylor
    0

    This is what I was looking for:

    var items = from blockListItems in this.Value<Umbraco.Core.Models.Blocks.BlockListModel>("myBlockList") select blockListItems.Content as ContentModels.MyElementTypeAlias;
    
  • Casper 69 posts 305 karma points
    Oct 03, 2020 @ 10:55
    Casper
    100

    Is this what you wan't?

    Model.MyProperty.Select(x => (ContentModels.MyElementTypeAlias)x.Content)
    
  • Matt Taylor 859 posts 2052 karma points
    Oct 05, 2020 @ 17:04
    Matt Taylor
    0

    Yes, that's it.

    Thanks Casper.

  • Mia Martin 1 post 71 karma points
    Oct 03, 2020 @ 14:10
    Mia Martin
    0

    Thank you so much for putting this here as I was also looking for the same query.

Please Sign in or register to post replies

Write your reply to:

Draft