Copied to clipboard

Flag this post as spam?

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


  • Jesper Skjønnemand 39 posts 274 karma points
    Nov 30, 2018 @ 12:12
    Jesper Skjønnemand
    0

    Not having much success with rendering tags in my template.

    The (very simple) template looks something like this:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        Layout = "MasterTemplate.cshtml";
    }
    
    @{
        var topics = Model.Content.GetPropertyValue<string>("pageTopics");
        <p>This is a page about @topics</p>
    } 
    

    I hope to render something like

    This page is about TESTING, UMBRACO, CLOUD, TAGS

    But all I get is

    This page is about System.String[]

    What am I missing?

  • Dennis Aaen 4468 posts 18026 karma points admin hq c-trib
    Nov 30, 2018 @ 12:21
    Dennis Aaen
    0

    Hi Jesper,

    Can you please give this a go.

    @if(Model.Content.pageTopics.Any()){
        <ul>
            @foreach(var tag in Model.Content.pageTopics){
                <li>@tag</li>
            }
        </ul>
    }
    

    Also make sure that your property is called pageTopics and the property is on the page that you are render in the browser.

    Hope this helps,

    /Dennis

  • Jesper Skjønnemand 39 posts 274 karma points
    Nov 30, 2018 @ 12:30
    Jesper Skjønnemand
    0

    Hi Dennis

    When I insert this code, preview (Umbraco Canvas Designer) displays Server Error in '/' Application. Runtime Error and some more text in yellow boxes.

    I have triple (actually more) checked. The property name is pageTopics, and my test page contains tags.

    Tags are stored as JSON, not CSV.

    I am running Umbraco Cloud.

  • louisjrdev 107 posts 343 karma points c-trib
    Nov 30, 2018 @ 14:37
    louisjrdev
    2

    Can you try:

    @if(Model.Content.GetPropertyValue<IEnumerable<string>>("pageTopics") !=null){
    <ul>
        @foreach(var tag in Model.Content.GetPropertyValue<IEnumerable<string>>("pageTopics")){
            <li>@tag</li>
        }
    </ul>
    

    }

  • Rasmus Eeg 91 posts 456 karma points c-trib
    Dec 01, 2018 @ 15:38
    Rasmus Eeg
    100

    Hi Jesper,

    Here is how you should do it:

    @inherits Umbraco.Web.Mvc.UmbracoTemplatePage
    
    @{
        Layout = "MasterTemplate.cshtml";
        var topics = Model.Content.GetPropertyValue<string[]>("pageTopics");
    }
    
     <p>This is a page about @string.Join(", ", topics)</p>
    

    If you insist on the tags being uppercase, i would to the following:

    var topics = Model.Content.GetPropertyValue<string[]>("pageTopics").Select(x=> x.ToUpperInvariant());
    
  • Jesper Skjønnemand 39 posts 274 karma points
    Dec 02, 2018 @ 15:49
    Jesper Skjønnemand
    0

    Hi all

    Suggestions from Louis and Rasmus both worked. You just resqued my whole week. Thanks.

    Jesper

  • Liam Dilley 103 posts 271 karma points
    May 20, 2021 @ 00:26
    Liam Dilley
    0

    In Version 8 I can not get anything to work. To get the tags as a comma seperated string. I have tried a number of C# methods but you either get Umbraco errors or not the output you expect.

    string.Join(",", rpub.Value<string>("publicationTag"));
    

    This returns System.String[]

    var test2 = rpub.Value<string[]>("publicationTag");
    

    This returns the same thing - System.String[]

    var test3 = rpub.Value<IEnumerable<string>>("publicationTag");
    

    This returns System.String[]

    I just want to have a clean string of the tag.Text comma separated so I can then do a .Contains inside.

    Even:

     x.Value<string>("publicationTag").Contains(tag.Text)
    

    Trying to filter items based on tags with a certain text does not work.

    Tags in 8 seems a bit broken.

Please Sign in or register to post replies

Write your reply to:

Draft