我有一个非常烦人的问题,我认为:
@{
if(ViewBag.Section == "Home")
{
<div id="headerfrontPage">
}
else
{
<div id="header">
}
}
我得到一个编译错误:
The code 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.
如何有条件地编写一个 div?它基本上是为了黑客...
最佳答案
当你包裹你的 div 的内部元素时,你可以使用相同的结构,比如:
@if (ViewBag.Section == "Home")
{
<text><div id="headerfrontPage"></text>
}
else
{
<text><div id="header"></text>
}
或者你使用razor语法@: like
@if (ViewBag.Section == "Home")
{
@:<div id="headerfrontPage">
}
else
{
@:<div id="header">
}
但对于您目前的情况,我更喜欢 Ron Sijm 的解决方案:
@{
var divName = ViewBag.Section == "Home" ? "headerfrontPage" : "header";
}
<div id="@divName">
关于c# - 为什么 Razor 不喜欢这个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8040482/