我有几个<div>
组成一个菜单,用户可以选择其中一个选项。但是,有时候,该选项需要将用户带到另一个页面,即有时需要在其周围打开一个链接。
以下是迄今为止我的方法的简化版本:
@if (true)
{
<a href="@Url.Action("Details", "Item", new { id = 1 })">
}
<div>
A div with some stuff in it...
</div>
@if (true)
{
</a>
}
这会导致以下错误:
Parser Error Message:
The if block is missing a closing "}" character. Make sure you have a matching "}" character for all the "{" characters within this block, and that none of the "}" characters are being interpreted as markup.
我找到了this post,但它对我没有帮助,因为给出的答案使用Html.Raw()
和<text>
,这将阻止我在我的链接中使用@Url.Action()
。< / p>
有没有办法让这项工作或我需要做一些完全不同的事情?
答案 0 :(得分:4)
你可以做你喜欢的事情:
@if (true)
{
@Html.Raw("<a href='" + Url.Action("Details", "Item", new { id = 1 }) + "'>")
}
<div>
A div with some stuff in it...
</div>
@if (true)
{
@Html.Raw("</a>")
}
虽然,第二个选项可能更清晰,您可以将div和内容设置为局部视图:
@if (true)
{
<a href="@Url.Action("Details", "Item", new { id = 1 })">
@Html.Partial("PartialName")
</a>
}else {
@Html.Partial("PartialName")
}
第三种选择是编写自己的Html Helper。
答案 1 :(得分:0)
如线程Razor view will not allow to be split中所述,这可以在没有@Html.Raw()
的情况下实现,这意味着您的HTML不必以字符串结尾:
@if (1 == 1) {
@:<div>
}
<div>some content</div>
@if (1 == 1) {
@:</div>
}