剃刀编译错误

Dav*_*haw 4 c# asp.net-mvc razor

我有以下Razor代码部分,它在运行时因编译错误而失败:

@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>
        }
Run Code Online (Sandbox Code Playgroud)

错误就@stat.TimeTaken.Hours hours行了

CS1002 :; 预期

删除文字hours修复它.

我很困惑.

编辑:这是从"@if"到"秒"的编译输出.

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");
Run Code Online (Sandbox Code Playgroud)

Kel*_*sey 5

我想你需要摆脱@在线:

@if (@stat.TimeTaken.Hours > 0) { 
Run Code Online (Sandbox Code Playgroud)

做了:

@if (stat.TimeTaken.Hours > 0) { 
Run Code Online (Sandbox Code Playgroud)

编辑:只需查看ScottGu的博客(请参阅标识"识别嵌套内容"一节),这很有趣:

对于您希望在没有包装标记的情况下向客户端呈现内容的情况,您可以选择使用块包装嵌套内容:

并查看他下面的if语句的代码示例.

替代文字