在 href 标签上使用条件 html

Use a condition on href tag html

我有以下代码:

if (client.id != 0)
{
    var summaryLink = Url.Action("Add", "Client");
    <a href="@summaryLink">
        <div>
            <h3>Client</h3>
        </div>
    </a>
}
else
{
    <a>
        <div class="mdl-tabs__title">
            <h3>Client</h3>
        </div>
    </a>
}

这行得通,但我不想在标签内重复代码,而是根据条件添加 href。

知道我该怎么做吗?

你可以使用三元运算符吗?见 docs also using a hash won't cause a redirect you could use an empty string or some other alternative see

var summaryLink = client.id != 0 ? Url.Action("Add", "Client") : "#";
<a href="@summaryLink">
  <div>
    <h3>Client</h3>
  </div>
</a>