【发布时间】:2016-11-16 17:36:45
【问题描述】:
我在 VS2015 的 cshtml Razor 视图中遇到了一个奇怪的问题。我认为这一定是 Razor 错误,但如果可能的话,希望能进行健全性检查和解决方法的建议。
这个问题是 @{} 代码块中的花括号没有正确解析。我在foreach 循环中有一个if 语句,如果我使用花括号包围if 操作,我会收到编译错误。有趣的是,else 语句后的花括号看起来很好。
使用 VS 颜色编码会更容易演示,但这里是这样。
这行得通:
@{
var nestLevel = 0;
foreach (var t in xmlHelper.GetProperties(asm, ViewBag.xmlType, "root"))
{
var type = @t.Item3.PropertyType;
if (xmlHelper.builtInTypes.Contains(type.ToString()))
<p>@t.Item1 @t.Item2 @t.Item3.PropertyType</p>
else
{
nestLevel++;
}
}
} //VS shows the @{} code block ending here as expected
但是,如果我现在在 if 操作周围添加花括号,它将无法编译:
@{
var nestLevel = 0;
foreach (var t in xmlHelper.GetProperties(asm, ViewBag.xmlType, "root"))
{
var type = @t.Item3.PropertyType;
if (xmlHelper.builtInTypes.Contains(type.ToString()))
{
<p>@t.Item1 @t.Item2 @t.Item3.PropertyType</p>
}
else
{
nestLevel++;
}
} //VS now incorrectly shows the @{} code block ending here
}
有什么想法/建议吗?
【问题讨论】:
标签: c# asp.net-mvc razor