Copied to clipboard

Flag this post as spam?

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


  • Laurence Gillian 600 posts 1219 karma points
    Dec 13, 2016 @ 15:43
    Laurence Gillian
    0

    GetValue returning 'system.int32[]', rather than Value

    Use case: Storing a collection of Images (e.g. array on Ints)
    When using GetValue the type is returned rather than the items.

    I think this is caused by having the Umbraco Core Property Value Converters installed.

    It took me a while to figure it out, so I thought I'd share it here too.

    Solution either type it correctly for the thing you are working with, or use the GetRawValue method.

    Get Value

    foreach (var item in Model.Items.Where(x => x != null))
    {
        var test = item.GetValue<string>("images");
        @test // outputs: system.int32[]
    }
    

    GetValue IEnum

    foreach (var item in Model.Items.Where(x => x != null))
    {
        var test = item.GetValue<IEnumerable<int>>("images");
        foreach (var x in test)
        {
            @x // outputs: 112711261128
        }
    }
    

    GetRawValue

    foreach (var item in Model.Items.Where(x => x != null))
    {
        var test = item.GetRawValue("images");
        @test // outputs: 1127,1126,1128
    }
    

    Git Commit with GetRawValue

  • 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