Copied to clipboard

Flag this post as spam?

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


  • Simon 5 posts 55 karma points
    Jun 19, 2014 @ 13:03
    Simon
    0

    foreach loop displays text separator twice?

    First of all, I am completely new to Umbraco and relatively new to MVC and Razor, so apologies if I come across as a complete noob...

    One of my Document Types has a Multiple Textstring property called "Key Features".

    On my template I want to loop through this property and display each textstring, comma-separating the list to make it read like a sentence.  i.e. one comma between each item and no comma after the last item.

    Here's my code fragment:

     

    var counter = 1;
    foreach( var feature in property.AsDynamic().keyFeatures)
    {
        @feature
        if (counter < property.AsDynamic().keyFeatures.Length)
        {
            <span>,</span>
        }
        counter++;
    }                                                   

    The output is:

    feature1
    <span>,</span>
    <span>,</span>
    feature2
    <span>,</span>
    <span>,</span>
    feature3
    <span>,</span>
    <span>,</span>
    feature4

    So the if statement handles the condition in the correct way, but why on earth is it outputting the <span> twice!!!

    I'm really hoping this is a simple "you stupid noob, you should have done this" problem.

     

    Cheers,

    Simon

     

  • Simon 5 posts 55 karma points
    Jun 19, 2014 @ 13:16
    Simon
    100

    Stop the press!   Now it suddenly works!  I can't see what has occurred - my code is unchanged.

    I published the page again, but previously changes to the template were being shown on screen. 

    (Everyone else walks away, rolling their eyes...)

  • Steve Morgan 1349 posts 4458 karma points c-trib
    Jun 19, 2014 @ 13:30
    Steve Morgan
    0

    I am fairly sure there is a weird save bug when you've got an synatically invalid Razor macor that occassionaly you make a change - hit save, receive a confirmation but it doesn't change the macro. This might be what you saw (as the symptons match some of my own)..  if you can you nail down the process I'd love to trap and report this bug (and convince myself I'm not going mad!). 

  • Simon 5 posts 55 karma points
    Jun 19, 2014 @ 13:33
    Simon
    0

    Thanks Steve, I'll be sure to keep a watchful eye out for similar issues.

Please Sign in or register to post replies

Write your reply to:

Draft