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.
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
Have you tried saving the Razor file in Visual Studio? Maybe than it just works.
Jeroen
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
Hmm I don't see the problem either. Does this work?
Jeroen
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
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
Hmm that should just work. Here are some options which might work:
Or
Jeroen
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
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 "@:"
@: <br /> Phone: @Model.memberPhone
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*@
}
}
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>
}
}
}
Prettier if-statement:
@if(parent!=null && parent.Children.Any())
{
<div id="sidenav">
@ulMenu(parent, startLevel, finishLevel)
</div>
}
Try removing the @ from @Model.AncestorOrSelf(startLevel);
Jeroen
is working on a reply...