Copied to clipboard

Flag this post as spam?

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


  • Tim C 161 posts 528 karma points
    May 19, 2016 @ 13:44
    Tim C
    0

    Syntax of @

    I had a problem posted earlier about { } but which I think is now something to do with my misunderstanding how the @ directive works in razor. Example

    @{
        var noticeType = "";
        var nt = @Umbraco.Field("noticeType").ToString();
    
         switch(nt){
            case "1" : noticeType = "AAA";
            break;
            case "2" : noticeType = "BBB";
            break;
            case "3" : noticeType = "CCC";
            break;
    
    
    }
    
    }
    

    causes error

    Compiler Error Message: CS1513: } expected

    Source Error:

    Line 79: } Line 80: } Line 81: }

    However, if I separate the Razor into two blocks with the switch statement not in a @ block

    @{
        var noticeType = "";
        var nt = @Umbraco.Field("noticeType").ToString();
    }
    @switch(nt){
            case "1" : noticeType = "AAA";
            break;
            case "2" : noticeType = "BBB";
            break;
            case "3" : noticeType = "CCC";
            break;
        }
    

    it's fine.

    Can someone explain why

    @switch{
    ...
    }
    

    is fine but

    @{
    switch{
    ...
    }
    }
    

    isn't? I thought surrounding text with @{ } meant 'anything within this is to be treated as Razor'?

    Very confusing!!

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    May 19, 2016 @ 13:49
    Dennis Adolfi
    0

    Hi Tim.

    You are correct, but your first code example is missing a } in the end.

    @{ <!-- Here you open a bracket
        var noticeType = "";
        var nt = @Umbraco.Field("noticeType").ToString();
    
         switch(nt){ <!-- Here you open another bracket
            case "1" : noticeType = "AAA";
            break;
            case "2" : noticeType = "BBB";
            break;
            case "3" : noticeType = "CCC";
            break;
        } <!-- Here you close a bracket (the switch)
    <!-- Here you should close a bracket
    

    Makes sense?

    Other than that you are correct about text surrounded with @{ }.

  • Tim C 161 posts 528 karma points
    May 19, 2016 @ 14:03
    Tim C
    0

    Dennis

    Thank you for the quick repsonse. However, I wasn't missing a } in my code - I just missed it when I copied it to this entry - my apologies.

    I have corrected the original post.

    I have tried again (just in case I was missing the closing } but I still get the crash. It must be something else.

  • Nik 1625 posts 7295 karma points MVP 7x c-trib
    May 19, 2016 @ 14:06
    Nik
    1

    Hi Tim,

    I would try taking the @ away from the @Umbraco line.

    I've used switches within Razor code blocks before and not had an issue so it does seem a bit strange.

    Nik

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    May 19, 2016 @ 14:09
    Dennis Adolfi
    100

    As Nik said:

    Removing the @ before Umbraco.Field("noticeType").ToString() solves it. Sorry i missed that..

    Thank you Nik!

  • Tim C 161 posts 528 karma points
    May 19, 2016 @ 14:11
    Tim C
    1

    That's it - presumably the @Umbraco... thinks it is starting a new block.

    Lesson learned!

  • Dennis Adolfi 1082 posts 6450 karma points MVP 6x c-trib
    May 19, 2016 @ 14:12
    Dennis Adolfi
    0

    That sounds logical.

    Have a great day Tim! Good luck with the rest of the site!!

  • 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