Mat*_*tio 19 razor asp.net-mvc-3
我最近将一个项目从MVC 1升级到MVC 3,现在我正在尝试使用Razor.
在一个视图中,我有一个foreach代码块,但嵌套的if语句似乎不希望它前面的@.
我原来的代码是:
@foreach(var r in Model.Results)
{
string css = r.Result.Count() > 0 ? "fail" : "pass";
<p class="@css"><strong>@r.Description</strong></p>
@if(r.Result.Count() > 0)
{
<p>Count: @r.Result.Count()</p>
<table>
<thead>
<tr>
<th>ID</th><th>Title</th><th>Description</th>
</tr>
</thead>
<tbody>
@foreach(var e in r.Result) {
<tr><td>@e.Id</td><td>@e.Title</td><td>@e.Description</td></tr>
}
</tbody>
</table>
}
}
Run Code Online (Sandbox Code Playgroud)
我将在@if中遇到运行时错误:在"@"字符后面出现意外的"if"关键字.进入代码后,您不需要使用"@"作为"if"等构造的前缀.
如果我删除@代码运行正常.我期望需要@因为它前面的HTML.更令我困惑的是,在嵌套的foreach之前我确实需要@.这里有什么规则?
lan*_*der 40
在剃刀的任何括号内,它需要匹配的开始和结束标记.这就是解析器的工作原理.
到目前为止,以下示例有效:
@for (var i = 0; i < 10; i++) {
<p>
@i.ToString()
</p>
}
Run Code Online (Sandbox Code Playgroud)
这不是:
@for (var i = 0; i < 10; i++) {
<p>
@i.ToString()
</p>
@if (i == 2) {
<p>2</p>
}
}
Run Code Online (Sandbox Code Playgroud)
要解决这个问题,您可以将其置于以下<text>块中:
@for (var i = 0; i < 10; i++) {
<text>
<p>
@i.ToString()
</p>
@if (i == 2) {
<p>2</p>
}
</text>
}
Run Code Online (Sandbox Code Playgroud)
所以在你的情况下它会变成:
@foreach(var r in Model.Results)
{
@string css = r.Result.Count() > 0 ? "fail" : "pass";
<text>
<p class="@css"><strong>@r.Description</strong></p>
@if(r.Result.Count() > 0)
{
<p>Count: @r.Result.Count()</p>
<table>
<thead>
<tr>
<th>ID</th><th>Title</th><th>Description</th>
</tr>
</thead>
<tbody>
@foreach(var e in r.Result) {
<tr><td>@e.Id</td><td>@e.Title</td><td>@e.Description</td></tr>
}
</tbody>
</table>
}
</text>
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25335 次 |
| 最近记录: |