我想根据变量是否设置来修改div。
所以我想做这样的事情:
@if (SomethingIsSet) { <div style="background:red"> } else { <div style="background:blue"> }
但我在 Visual Studio 中收到以下错误消息:
div 元素未关闭。所有元素必须是 自动关闭或有匹配的结束标记。
我的 div 元素稍后在页面中关闭。
您可以在 Razor 中使用三元运算符。
<div style="@(SomethingIsSet ? "background:red" : "background:blue")">