gpt4 book ai didi

c# - 使用 Razor Helper 有条件地包装内容

转载 作者:太空狗 更新时间:2023-10-30 01:23:58 25 4
gpt4 key购买 nike

我想使用 Razor 有条件地将一些内容包装在 <span> 中基于我模型的 bool 属性的元素。我的猜测是我需要使用 Templated Razor Delegates ,但我发现很难让它们正确。

我的模型是这样的:

public class Foo
{
public bool IsBar { get; set; }
}

在我看来,我希望能够使用类似于以下内容的东西:

<a href="/baz">
@Html.WrapWith(Model.IsBar, "span", @This content may be wrapped, or not)
</a>

渲染位置:

<!-- Model.IsBar == True -->
<a href="/baz">
<span>This content may be wrapped, or not</span>
</a>

<!-- Model.IsBar == False-->
<a href="/baz">
This content may be wrapped, or not
</a>

最佳答案

我总是用 span 包装内容,让 css 处理所有的表现逻辑,如果有的话

<a href="/baz">
<span class="@(Model.IsBar ? "bar" : "")">This content may be wrapped, or not</span>
</a>

并将CSS规则应用于.bar

span.bar
{
//some style rules
}

关于c# - 使用 Razor Helper 有条件地包装内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10687417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com