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");
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.
Try:
@if (stat.TimeTaken.Hours > 0) {
<text>@stat.TimeTaken.Hours hours</text>
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With