Copied to clipboard

Flag this post as spam?

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


  • Craig O'Mahony 364 posts 918 karma points
    Aug 14, 2018 @ 13:05
    Craig O'Mahony
    0

    Razor Syntax Error

    Hi all,

    I've put together a fairly simple razor script where I'm building up a string and attempting to output the result to the page but I'm getting an error that I'm missing a } and for the life of me I can't see what's wrong. Could anyone assist please?

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
            var folderNode = Umbraco.TypedContent(1068);
            var playList = "";
            var counter = 0;
            foreach(var node in folderNode.Children)
            {
                playList = "'" + @node.Name + "':[";
                foreach(var song in node.Children)
                {
                    playList = playList + counter.ToString() + ",";
                    counter++;
                }
    
                playList += "],";
            }
            @Html.Raw(playList);
    }
    

    Thanks, C

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    Aug 14, 2018 @ 13:14
    Nik
    0

    Hi Craig,

    I think the error about a missing } is possibly a bit misleading.

    The first thing that jumped out to me was this line here:

    playList = "'" + @node.Name + "':[";
    

    I'm pretty sure you don't need the @ before node.Name

    Try removing that and see if it decides to behave for you.

    Cheers

    Nik

  • suman 30 posts 101 karma points
    Aug 14, 2018 @ 13:21
    suman
    0

    Hi Craig,

    Nik is correct.. below are your updated code.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage

    @{

    var folderNode = Umbraco.TypedContent(1068);
    var playList = "";
    var counter = 0;
    foreach (var node in folderNode.Children)
    {
        playList = "'" + node.Name + "':[";
        foreach (var song in node.Children)
        {
            playList = playList + counter.ToString() + ",";
            counter++;
        }
    
        playList += "],";
    

    }

    @Html.Raw(playList);

    }

  • Craig O'Mahony 364 posts 918 karma points
    Aug 14, 2018 @ 13:24
    Craig O'Mahony
    0

    Awesome! Couldn't see the wood for the trees there.

    Thanks folks!

  • 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