Copied to clipboard

Flag this post as spam?

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


  • Anthony Candaele 1197 posts 2049 karma points
    Apr 25, 2012 @ 16:16
    Anthony Candaele
    0

    issue with Razor if statement

    Hi Guys,

    This kinda drives me nuts:

    I have a Razor script:

    <h1>@Model.Name</h1>
    <h2>Contact</h2>
    <p>@Model.memberEmail

    @if(Model.GetProperty("memberPhone").Value != null)
      {
       <br />Phone@Model.memberPhone
      }
     
    </p>

    when I try to save this script, I get this error message:

    error CS1002: ; expected

    When I omit the '@' before the if-statement, then I can save the script, but the if-statement doesn't get processes.

    Anyone else encounterd this?

    Thanks for your help,
    Anthony

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Apr 25, 2012 @ 16:41
    Jeroen Breuer
    1

    Have you tried saving the Razor file in Visual Studio? Maybe than it just works.

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 25, 2012 @ 16:52
    Anthony Candaele
    0

    Hi Jeroen,

    Yes I tried that, but without luck. The odd thing is that in VS, I also get a squigly at the end of the line:

    <br />Phone: @Model.memberPhone

    telling me to that a ';' is expected

    thanks for your help,

    Anthony

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Apr 25, 2012 @ 16:54
    Jeroen Breuer
    1

    Hmm I don't see the problem either. Does this work?

    <br />Phone: @(Model.memberPhone)

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 26, 2012 @ 09:08
    Anthony Candaele
    0

    Hi Jeroen,

    I deleted code in the if-statement and typed it again and now it works. It must have been an hidden character or something.

    Thanks for your help,

    Anthony

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 26, 2012 @ 09:21
    Anthony Candaele
    0

    I guess I still need to unravel the mysteries of Razor, this code works:

    @if (Model.GetProperty("memberPhone").Value != null)
    {
        <br />@Model.memberPhone
    }


    But if I add the static text 'Phone':

    @if (Model.GetProperty("memberPhone").Value != null)
    {
        <br />Phone: @Model.memberPhone
    }

    c:\Repos\creative-website\testcreative\macroScripts\634710288041184781_Member.cshtml(9): error CS1002: ; expected

    Does someone know what's going on??

    Thanks for your help,
    Anthony

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Apr 26, 2012 @ 09:23
    Jeroen Breuer
    1

    Hmm that should just work. Here are some options which might work:

    @if (Model.GetProperty("memberPhone").Value != null)
    {
        <text><br />Phone:</text> @Model.memberPhone
    }

    Or

    @if (Model.GetProperty("memberPhone").Value != null)
    {
        @("<br />Phone:" + Model.memberPhone)
    }

    Jeroen

  • Anthony Candaele 1197 posts 2049 karma points
    Apr 26, 2012 @ 09:29
    Anthony Candaele
    0

    Hi Jeroen,

    Yes, both options work, the second one needed a little tweaking because the '<br />' was not interpreted but rendered literaly, so I changed it to:

    <br /> @("Phone:" + Model.memberPhone)

    That also worked fine, thanks for your help,

    Anthony

  • AndersM 6 posts 26 karma points
    Apr 26, 2012 @ 09:29
    AndersM
    0

    If you want a nice syntax, look at this: http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

    This works as intended:

       @{
            var test = "hello";
        }
        @if (1 == 1)
        {
            @:<br /><br /><br /> @test
        }  

    Tldr: You just need to use "@:"

  • AndersM 6 posts 26 karma points
    Apr 26, 2012 @ 09:33
    AndersM
    0

    @: <br /> Phone: @Model.memberPhone

  • Andrew 14 posts 34 karma points
    Aug 16, 2013 @ 09:49
    Andrew
    0

    I am having this same issue, I am trying to use some razor scripts from an earlier version of Umbraco and I'm getting an error when saving the file saying it expects a '}'

     

    The snippet if below and it appears to be the if statement that is breaking it, any ideas on what has changed or what I'm doing wrong? I was just trying to port this code from 4.* to the latest 4.* to get the side navigation working.

     

    @{ 

      var startLevel = 2;

      var finishLevel = 4;   

      var parent = @Model.AncestorOrSelf(startLevel);

    if(parent!=null)

    {

    var childCount = parent.Children.Count();

    @* DO some other logic*@

    }

    }

  • Andrew 14 posts 34 karma points
    Aug 16, 2013 @ 09:53
    Andrew
    0

    OK so I just tried splitting down my if statement from the variables like below and got it working! Typical just after I had posted ;)

     

    @{ 

      var startLevel = 2;

      var finishLevel = 4;   

      var parent = @Model.AncestorOrSelf(startLevel);

    }

     

    @if(parent!=null){ 

     var childCount = parent.Children.Count();

     if(childCount>0)

     {

     <div id="sidenav">

     @ulMenu(parent, startLevel, finishLevel)

       </div>

     }

      }

    }

  • AndersM 6 posts 26 karma points
    Aug 16, 2013 @ 15:45
    AndersM
    0

    Prettier if-statement:

    @if(parent!=null && parent.Children.Any())

        <div id="sidenav">

            @ulMenu(parent, startLevel, finishLevel)

        </div>

    }

  • Jeroen Breuer 4861 posts 12138 karma points MVP 3x admin c-trib
    Aug 20, 2013 @ 12:04
    Jeroen Breuer
    0

    Try removing the @ from @Model.AncestorOrSelf(startLevel);

    Jeroen

  • 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