Copied to clipboard

Flag this post as spam?

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


  • Hubert Thalmann 57 posts 263 karma points
    Sep 08, 2016 @ 06:51
    Hubert Thalmann
    0

    if/else problem

    hello Our.Umbraco

    I got a problem with my macro.

    Here's the Code

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    @{ var selection = CurrentPage.Children.Where("Visible"); }
    
    @if (selection.Any())
    {
        var count = 0;
    
            foreach (var item in selection)
            {
                if(count % 2 == 0) 
                {
                <div class="row">           
                <div class="col-sm-1 col-xs-0">
                </div>
                <div class="col-sm-5 col-xs-12">
                <p class="text">@item.Datum</p>
                <label class="subtitle">@item.Anlass</label><br /><br />
                </div>
                }
                else
                {
                    <div class="col-sm-5 col-xs-12 text">
                        <p class="text">@item.Datum</p>
                        <label class="subtitle">@item.Anlass</label><br /><br />
                    </div>
                </div>
                }
            count++;
            }
    }
    

    my problem is, it doesn't read the "else" statement. It puts out "} else {". It looks like its putting it out as html and not reading it as a macro.

    What did i do wrong?

  • Hubert Thalmann 57 posts 263 karma points
    Sep 08, 2016 @ 07:10
    Hubert Thalmann
    0

    i think i got it.. i have to put @: in front of every code that is HTML. like:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    
    @{ var selection = CurrentPage.Children.Where("Visible"); }
    
    @if (selection.Any())
    {
        var count = 0;
    
            foreach (var item in selection)
            {
                if(count % 2 == 0) 
                {
                @:<div class="row">         
                @:<div class="col-sm-1 col-xs-0">
                @:</div>
                @:<div class="col-sm-5 col-xs-12">
                @:<p class="text">@item.Datum</p>
                @:<label class="subtitle">@item.Anlass</label><br /><br />
                @:</div>
                }
                else
                {
                    @:<div class="col-sm-5 col-xs-12 text">
                        @:<p class="text">@item.Datum</p>
                        @:<label class="subtitle">@item.Anlass</label><br /><br />
                    @:</div>
                @:</div>
                }
            count++;
            }
    }
    
  • Kevin Jump 2342 posts 14889 karma points MVP 8x c-trib
    Sep 08, 2016 @ 08:00
    Kevin Jump
    100

    Hi Hubert

    that is an intresting solution i never knew you could do that. :)

    but it should work the other way.

    looking at the code i think it's because you have a missing / extra div inside the if (relating to the <div class="row">)

    if you move the first div outside of if and the lass div from the else.

       foreach (var item in selection)
            {
                <div class="row">           
                  @if(count % 2 == 0) 
                  {
                    <div class="col-sm-1 col-xs-0">
                    </div>
                    <div class="col-sm-5 col-xs-12">
                       <p class="text">@item.Datum</p>
                       <label class="subtitle">@item.Anlass</label><br /><br />
                    </div>
                  }
                  else
                  {
                    <div class="col-sm-5 col-xs-12 text">
                        <p class="text">@item.Datum</p>
                        <label class="subtitle">@item.Anlass</label><br /><br />
                    </div>
                  }
                </div>
    

    then that should work (note the @ in front of the if statement now because it's inside the div)

Please Sign in or register to post replies

Write your reply to:

Draft