由Visual Studio搭建的Razor视图包含一个"actions"元素,其链接由管道字符(|
)分隔
<td>
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
</td>
我想有条件地呈现这些链接:
<td>
@if (item.IsSuccess)
{
@Html.ActionLink("Edit", "Edit", new { id = item.Id }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
}
</td>
上面的代码在Visual Studio中似乎是正确的,但是执行时会导致Compilation Error
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS1513: Expected sign }. Source Error: Line 491: } Line 492: } Line 493:}
你能告诉我问题在哪里吗?代码语法似乎是正确的。
一旦进入c#块,必须再次显式地退出。在这种情况下,您可以使用<text>
标记向输出添加一行或多行文字。
<td>
@if (item.IsSuccess)
{
@Html.ActionLink("Edit", "Edit", new { id = item.Id })<text> |</text>
@Html.ActionLink("Details", "Details", new { id = item.Id })<text> |</text>
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
}
</td>
或者如John H所提到的,您可以使用语法@:
将代码块分隔为单行文本。
<td>
@if (item.IsSuccess)
{
@Html.ActionLink("Edit", "Edit", new { id = item.Id })@: |
@Html.ActionLink("Details", "Details", new { id = item.Id })@: |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
}
</td>
参见在代码块中组合文本、标记和代码