c# - Blazor 关闭有条件的 div 包装器上的 div 标签

标签 c# html .net blazor blazor-server-side

我试图根据某些条件将我的组件之一包装在某个特定标签上。

为了简单起见,假设我有一个

<div class="inner-class">
    this must be in inner-class div and wrapped in some-class div
</div>

如果 'condition' == true 那么它应该将它包装在另一个 div 中,结果应该是这样的

<div class="wrapper-class">
    <div class="inner-class">
        this must be in inner-class div and wrapped in some-class div
    </div>
</div>

在你说之前,使用 if-else 方法。我必须说不。因为标签和其中的类是动态的,所以我不能那样写。

我想做的是

@if (condition)
{
    @:<div class="wrapper-class">
}
    <div class="inner-class">
        this must be in inner-class div and wrapped in some-class div
    </div>
}
@if (condition)
{
    @:</div>
}

我认为它应该完成这项工作。 但问题是浏览器在将内部 div 放入其中之前关闭了外部 div。

enter image description here

最佳答案

你必须使用BuildRenderTree

使用 BuilderRenderTree,您可以完全控制构建组件 html。

有关更多信息,请阅读这篇好文章 Building Components Manually via RenderTreeBuilder

关于c# - Blazor 关闭有条件的 div 包装器上的 div 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67367345/

相关文章:

java - 我如何从 html 中获取细节?

.net - vsto 中唯一的 Excel 工作簿标识符

c# - .NET 程序集绑定(bind),我可以将一个程序集映射到另一个程序集中的版本吗?

c# - 使用 token 通过 OAuth2 在 Twitter 上进行搜索

c# - 带有 Vuforia SDK 的 Unity3d 中 ARCamera 的 Raycast 问题

c# - MouseDown 事件和进度条

html - 复选框大小在 IE7 中有所不同

c# - 为什么这个 'Label' 是一个字段?

html - Logo 应该是 html 电子邮件中的单独图像吗?

.NET:将程序集绑定(bind)到引用程序集的新版本,无需重新编译