Copied to clipboard

Flag this post as spam?

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


  • Nicolai 48 posts 201 karma points
    Sep 04, 2015 @ 10:32
    Nicolai
    0

    Multiple Else if inside if statement. Umbraco Code interpretation

    Hey guys, How would one write this correctly?

       @if (x = y) {
           // Do something
       } else if (x = 1) {
          // Do something else
       } else if (x = 2) {
          // Do something wild
       } else {
         // Do something wildly
       }
    

    The last else if and the else will not be interpreted as code and I cant figure where to place the extra @ tag. Can anyone help me?

    Full code is:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
        <h2>ARTIKLER</h2>
    @{
    
        var years = Umbraco.Content(3772).Children;
        for (int i = 0; i < years.Count(); i++) {
    
            var page = years[i];
    
            if (i < 3) {
                <a class="btn btn-primary" href="@page.Url">@page.Name</a>
            } else if (i == 3) {
                <label id="lblSelect">
                    <select id="selectPointOfInterest" title="Vælg årstal" onchange="location = this.value;">
                        <option value="@page.Url">@page.Name</option>
            } else if (i < years.Count() && i > 3) {
                        <option value="@page.Url">@page.Name</option>
            } else if (i == years.Count()-1) {
                        <option value="@page.Url">@page.Name</option>
                    </select>
                </label>
            }
        }
    }
    
  • Gary Devenay 39 posts 245 karma points
    Sep 04, 2015 @ 12:30
    Gary Devenay
    0

    Hi Nicolai,

    When you have multiple else if statements it's sometimes better to use a switch https://msdn.microsoft.com/en-us/library/06tc147t.aspx

    You code may look something like the following:

    @{
        switch(x)
        {
            case 1:
                // Do something
                break;
            case 2:
                // Do something
                break;
            default:
                // Do something
                break;
        }
    }
    

    The default statement in the switch acts the same as your intended behaviour for else. Try giving it a shot as it's probably better practice than multiple else if's.

    Gary

  • Nicolai 48 posts 201 karma points
    Sep 04, 2015 @ 12:38
    Nicolai
    0

    Hi Gary,

    Thanks for trying to help! Im afraid I cant use switch case statements in this particularly situation because I need the logical operations.

    I have added the full code example to my main post.

    Regards, Nicolai

  • Alessandro Calzavara 32 posts 144 karma points c-trib
    Sep 04, 2015 @ 13:02
    Alessandro Calzavara
    0

    The problem with the example is opening some html tags withouth closing them in the same branch of the IF.

    Try generating a string of your html and then printing it out

  • Nicolai 48 posts 201 karma points
    Sep 18, 2015 @ 08:45
    Nicolai
    100

    I found a solution to this problem if anyone is interested :-)

    <h2>PAGE</h2>
        @{
    
            var years = Umbraco.Content(3772).Children;
            for (int i = 0; i < years.Count(); i++) {
    
                var page = years[i];
    
                if (i < 3) {
                    <a class="btn btn-primary" href="@page.Url">@page.Name</a>
                } else if (i == 3) {
                    @:<label id="lblSelect">
                        @:<select id="selectPointOfInterest" title="Vælg årstal" onchange="location = this.value;">
                            <option value="@page.Url">@page.Name</option>
                } else if (i < years.Count() && i > 3) {
                            <option value="@page.Url">@page.Name</option>
                } else if (i == years.Count()-1) {
                            <option value="@page.Url">@page.Name</option>
                        @:</select>
                    @:</label>
                }
            }
        }
    
Please Sign in or register to post replies

Write your reply to:

Draft