views:

56

answers:

2

I have the following section of Razor code and it fails during runtime with a compilation error:

@foreach(var stat in Model){
            <li>
                @stat.EffectiveDate.ToShortDateString() - @stat.EventType.Description <br />
                TimeTaken: 
                @if (@stat.TimeTaken.Hours > 0) {
                    @stat.TimeTaken.Hours hours
                }
                @stat.TimeTaken.Minutes minutes 
                @stat.TimeTaken.Seconds seconds.
            </li>
        }

The error is on the @stat.TimeTaken.Hours hours line

CS1002: ; expected

removing the literal hours fixes it.

I'm baffled.

EDIT: Here's the compilation output from the "@if" to "seconds".

Line 180:       if (@stat.TimeTaken.Hours > 0) {
Line 181:                   
Line 182:              
Line 183:              #line default
Line 184:              #line hidden
Line 185:              
Line 186:              #line 29 "c:\documents and settings\dave\my documents\visual studio 2010\Projects\SportStats\SportStats\Views\Stats\Index.cshtml"
Line 187:  this.Write(stat.TimeTaken.Hours);
Line 188:              
Line 189:              #line default
Line 190:              #line hidden
Line 191:              
Line 192:              #line 29 "c:\documents and settings\dave\my documents\visual studio 2010\Projects\SportStats\SportStats\Views\Stats\Index.cshtml"
Line 193:                             hours
Line 194:               }
Line 195:  
Line 196:              
Line 197:              #line default
Line 198:              #line hidden
Line 199:              
Line 200:              #line 31 "c:\documents and settings\dave\my documents\visual studio 2010\Projects\SportStats\SportStats\Views\Stats\Index.cshtml"
Line 201:              this.WriteLiteral("\t\t\t\t");
Line 202:              
Line 203:              #line default
Line 204:              #line hidden
Line 205:              
Line 206:              #line 31 "c:\documents and settings\dave\my documents\visual studio 2010\Projects\SportStats\SportStats\Views\Stats\Index.cshtml"
Line 207:  this.Write(stat.TimeTaken.Minutes);
Line 208:              
Line 209:              #line default
Line 210:              #line hidden
Line 211:              
Line 212:              #line 31 "c:\documents and settings\dave\my documents\visual studio 2010\Projects\SportStats\SportStats\Views\Stats\Index.cshtml"
Line 213:              this.WriteLiteral(" minutes \r\n\t\t\t\t");
Line 214:              
Line 215:              #line default
Line 216:              #line hidden
Line 217:              
Line 218:              #line 32 "c:\documents and settings\dave\my documents\visual studio 2010\Projects\SportStats\SportStats\Views\Stats\Index.cshtml"
Line 219:  this.Write(stat.TimeTaken.Seconds);
Line 220:              
Line 221:              #line default
Line 222:              #line hidden
Line 223:              
Line 224:              #line 32 "c:\documents and settings\dave\my documents\visual studio 2010\Projects\SportStats\SportStats\Views\Stats\Index.cshtml"
Line 225:              this.WriteLiteral(" seconds.\r\n\t\t\t</li>\r\n");
+2  A: 

I think you need to get rid of the @ on line:

@if (@stat.TimeTaken.Hours > 0) { 

Make it:

@if (stat.TimeTaken.Hours > 0) { 

Edit: Just lookin at ScottGu's blog (see the section marked "Identifying Nested Content") and this was of interest:

You can optionally wrap nested content with a block for cases where you have content that you want to render to the client without a wrapping tag:

And check out his code example of the if statement directly below it.

alt text

Kelsey
Nope, same problem.Thanks for the correction though :)
DaveShaw
@DaveShaw so what compile error are you getting now as it should be different since your editted output is still showing the error in the if statement: `Line 180: if (@stat.TimeTaken.Hours > 0) {`
Kelsey
@Kelsey: Putting an `@` in front of an identifier in C# is actually perfectly valid and shouldn't change anything ("verbatim identifier", http://msdn.microsoft.com/en-us/library/aa664670(VS.71).aspx )
dtb
@dtb I have actually never used Razor but have been following it so I was trying to figure it out as I was going along :)
Kelsey
Thanks - guess I should re-read that ScottGu page :o
DaveShaw
+3  A: 

Try:

@if (stat.TimeTaken.Hours > 0) {
    <text>@stat.TimeTaken.Hours hours</text>
}
dtb
Yep, that did it.But why?
DaveShaw
My guess is the body of an `@if` is C#/Razor code; you need to switch back to "content mode" by wrapping the content in tags.
dtb
Thanks for the tip
DaveShaw
An alternative to wrapping in <text> tags would have been to prefix the literal string with "@:" - @stat.TimeTaken.Hours @:hours
MikeB