4

我有以下 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>
        }

错误@stat.TimeTaken.Hours hours在线

CS1002: ; 预期的

删除文字hours修复它。

我很困惑。

编辑:这是从“@if”到“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");
4

2 回答 2

5

我认为你需要摆脱@在线:

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

做了:

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

编辑:看看ScottGu 的博客(参见标记为“识别嵌套内容”的部分),这很有趣:

您可以选择使用块包装嵌套内容,以应对您想要在没有包装标签的情况下呈现给客户端的内容:

并查看其正下方的 if 语句的代码示例。

替代文字

于 2010-08-20T21:33:15.783 回答
4

尝试:

@if (stat.TimeTaken.Hours > 0) {
    <text>@stat.TimeTaken.Hours hours</text>
}
于 2010-08-20T21:44:04.727 回答