Copied to clipboard

Flag this post as spam?

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


  • Sam 7 posts 97 karma points
    May 01, 2016 @ 10:07
    Sam
    0

    Order child-items by custom numeric-field

    I'm using a simple foreach loop to recieve all the child items and than filter them by Alias:

    @foreach (var work in CurrentPage.Children)
    {
        if (work.ContentType.Alias == "work")
        {
            <p>@work.sortNumber</p> // numeric-field adjustable by customer
        }
    }
    

    I'm looking for a simple solution to sort the work collection by the work.sortNumber field. I think I came close to a solution but without a working result:

    foreach (var item in Model.Content.Children.Where(x => x.ContentType.Alias == "work").OrderBy(t => t.GetPropertyValue("sortNumber"))) {
        foreach (var property in item.Properties){
            // Cannot find a way to get the needed properties, and is this even a good way to do this?
        }
    }
    

    Any help would be appreciated!

  • Marc Goodson 2157 posts 14434 karma points MVP 9x c-trib
    May 01, 2016 @ 12:33
    Marc Goodson
    100

    Hi Sam

    In your loop you can read properties like so:

    @item.GetPropertyValue("workSortOrder")

    or

    @item.GetPropertyValue<int>("workSortOrder")

    If you need to cast the property to a type

  • Sam 7 posts 97 karma points
    May 01, 2016 @ 13:12
    Sam
    0

    I removed the inner foreach and replaced it with your suggested line of code: @item.GetPropertyValue("propertyValueNames") and it worked! Thanks.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies