Copied to clipboard

Flag this post as spam?

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


  • Johan Alkstål 12 posts 113 karma points
    Mar 09, 2018 @ 13:36
    Johan Alkstål
    0

    Getting tags field only gives me a System.string[] string

    I'm trying to render a list of article tags but all I get is a single string of System.string[].

    @if (Model.Content.HasValue("tags"))
    {
        var tags = Umbraco.Field("tags").ToHtmlString().Split(',');
    
        foreach (var tag in tags)
        {
            <div class="badge badge-info">@tag</div>
        }
    }
    

    I know the article has tags added to it. So what am I doing wrong?

  • Michaël Vanbrabandt 849 posts 3292 karma points c-trib
    Mar 09, 2018 @ 13:43
    Michaël Vanbrabandt
    0

    Hi Johan,

    you mean something like this:

    @if (Model.Content.HasValue("tags"))
    {
        string[] tagsList = Model.Content.Tags.ToString().Split(',');
    
        if (tagsList.Count() > 0)
        {
            @foreach (var tag in tagsList)
            {
                <div class="badge badge-info">@tag</div>
            }
        }
    }
    

    Hope this helps!

    /Michaël

  • Johan Alkstål 12 posts 113 karma points
    Mar 09, 2018 @ 13:52
    Johan Alkstål
    0

    Hey Michaël!

    I tried your version too with the same result.

    I get <div class="badge badge-info">System.string[]</div> rendered out. =/

  • Michaël Vanbrabandt 849 posts 3292 karma points c-trib
    Mar 09, 2018 @ 13:54
    Michaël Vanbrabandt
    0

    Johan,

    which version are you using?

    There has been a change in the property value convertors, can you try to disable them and try again:

    In umbracoSettings.config set EnablePropertyValueConverters to false.

    Hope this helps!

    /Michaël

  • Johan Alkstål 12 posts 113 karma points
    Mar 09, 2018 @ 13:56
    Johan Alkstål
    0

    It's 7.8.1.

    Setting that to false breaks the rendering of the pages.

  • Johan Alkstål 12 posts 113 karma points
    Mar 09, 2018 @ 14:06
    Johan Alkstål
    0

    It's 7.8.1.

    Setting that to false breaks the rendering of all pages.

  • Nik 1081 posts 4454 karma points MVP 2x c-trib
    Mar 09, 2018 @ 14:18
    Nik
    101

    Hi Johan,

    It's generally best to use the Property Value converters, it makes things alot easier.

    You should be able to simply do this:

    @if (Model.Content.HasValue("tags"))
    {
    
        if (Model.Content.Tags != null)
        {
            foreach (var tag in Model.Content.Tags)
            {
                <div class="badge badge-info">@tag</div>
            }
        }
    }
    

    This is because the conversion from a comma separated list is happening in the Property Conversion so you don't need to do anything with it. The property just gives you back the array of strings to iterate around :-)

    Thanks,

    Nik

  • Johan Alkstål 12 posts 113 karma points
    Mar 09, 2018 @ 14:32
    Johan Alkstål
    1

    That did it! Thanks Nik!

  • Nik 1081 posts 4454 karma points MVP 2x c-trib
    Mar 09, 2018 @ 14:59
    Nik
    0

    No problem, happy to help :-)

  • Michaël Vanbrabandt 849 posts 3292 karma points c-trib
    Mar 10, 2018 @ 06:23
    Michaël Vanbrabandt
    0

    Aha something new learned today! Thanks!

    /Michaël

  • Joop Visscher 2 posts 72 karma points
    Apr 29, 2018 @ 07:50
    Joop Visscher
    0

    I'm trying to do something similar but with no luck. This is my code:

     @{
                var selection = Model.Content.Site().FirstChild("container").Children().Where(x => x.IsVisible());
                    }
                    <div id="portfolio" class="portfolio grid-container portfolio-3 portfolio-masonry clearfix">                 
                        @foreach(var item in selection){
    
                            string imageUrl = item.GetPropertyValue<IPublishedContent>("rollupImage").Url;
                            var tags = item.GetPropertyValue<string>("tags");
                            foreach (var tag in tags)
                            {
                                <span>@tag</span>
                            }
    
                            <article class="portfolio-item pf-media pf-netherlands">
                                <div class="portfolio-image">
                                    <a href="portfolio-single.html">
                                        <img src="@imageUrl" />
                                    </a>
                                    <div class="portfolio-overlay">
                                        <a href="@imageUrl" class="left-icon" data-lightbox="image"><i class="icon-line-plus"></i></a>
                                        <a href="@item.Url" class="right-icon"><i class="icon-line-ellipsis"></i></a>
                                    </div>
                                </div>
                                <div class="portfolio-desc">
                                    <h3><a href="@item.Url">@item.Name</a></h3>
    
                                </div>
                            </article>
                        }
                    </div>
    

    But all I get is System.string[]

    What am I doing wrong here?

  • Nik 1081 posts 4454 karma points MVP 2x c-trib
    Apr 29, 2018 @ 08:37
    Nik
    1

    Hi Joop,

    The issue you have is most likely this:

    var tags = item.GetPropertyValue<string>("tags");
    

    You are explicitly telling the property converters to turn tags into a single string, when you actually want it to be an enumerable object.

    Try replacing it with this line:

    var tags = item.GetPropertyValue<string[]>("tags");
    

    and see how you get on.

    Thanks,

    Nik

  • Joop Visscher 2 posts 72 karma points
    Apr 29, 2018 @ 09:02
    Joop Visscher
    0

    Hi Nik,

    You're a life saver. Did this the trick!

    Thanks a bunch!!

    Joop

  • k 229 posts 593 karma points
    May 15, 2019 @ 16:46
    k
    0

    Hello,

    I am on Umbraco 8 and when rendering the list chosen from my checkboxlist is giving me : System.String[]

    Can someone please help.

    Thanks.

  • Søren Gregersen 296 posts 1159 karma points MVP c-trib
    May 15, 2019 @ 18:14
    Søren Gregersen
    0

    are you just calling .ToString() on your value?

    a System.String[] is an array, and you need to iterate it to render each value

    @foreach(var tag in [...]) {
        <a href="news/?tag=@tag">@tag</a>
    }
    
Please Sign in or register to post replies

Write your reply to:

Draft