我有几个<div>包含菜单的菜单,用户可以选择多个选项之一。然而,有时该选项需要将用户带到另一个页面,即有时需要有一个链接围绕它。
以下是迄今为止我的方法的简化版本:
@if (true)
{
<a href="@Url.Action("Details", "Item", new { id = 1 })">
}
<div>
A div with some stuff in it...
</div>
@if (true)
{
</a>
}
Run Code Online (Sandbox Code Playgroud)
这会导致以下错误:
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.
Run Code Online (Sandbox Code Playgroud)
我找到了这篇文章,但它对我没有帮助,因为给出的答案已使用Html.Raw(),这会阻止我在链接中<text>使用。@Url.Action()
有没有办法让这项工作成功,或者我需要做一些完全不同的事情?
你可以像这样做你所拥有的:
@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>")
}
Run Code Online (Sandbox Code Playgroud)
不过,第二个选项可能更清晰一些,您可以将 div 和内容设置为部分视图:
@if (true)
{
<a href="@Url.Action("Details", "Item", new { id = 1 })">
@Html.Partial("PartialName")
</a>
}else {
@Html.Partial("PartialName")
}
Run Code Online (Sandbox Code Playgroud)
第三种选择是编写您自己的 Html Helper。
| 归档时间: |
|
| 查看次数: |
2131 次 |
| 最近记录: |