C# 嵌套 if 语句使 Razor 感到困惑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16282889/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:38:11  来源:igfitidea点击:

Nested if statement is confusing Razor

c#asp.net-mvcrazor

提问by Derek

I'm trying to set up a dropdown menu that pulls from a Datatable. This works just fine for the first level of the menu.

我正在尝试设置一个从数据表中提取的下拉菜单。这适用于菜单的第一级。

Working code:

工作代码:

<ul class="dropdown-menu">
@foreach (System.Data.DataRow dr in menu.Rows)
{
    if (Level1 != dr["Level1"].ToString())
    {
        <li><a href="#">@dr["Level1"].ToString()</a></li>
        Level1 = @dr["Level1"].ToString();
    }
}
</ul>

The problem occurs when I try to add a nested if statement. If you put this code into Visual Studio you will notice that the closing bracket for the @foreachloop is not recognized by Razor.

当我尝试添加嵌套的 if 语句时会出现问题。如果将此代码放入 Visual Studio,您会注意到@foreachRazor 无法识别循环的右括号。

Code breaks:

代码中断:

<ul class="dropdown-menu">
@foreach (System.Data.DataRow dr in menu.Rows)
{
    if (Level1 != dr["Level1"].ToString())
    {
        <li><a href="#">@dr["Level1"].ToString()</a></li>
        Level1 = @dr["Level1"].ToString();

        if (Level2 != dr["Level2"].ToString())
        {
            <li><a href="#">@dr["Level2"].ToString()</a></li>
            Level2 = @dr["Level2"].ToString();                                        
        } 
    }
} <!-- the issue is the bracket on this line -->
</ul>

采纳答案by Splendor

You'll need to wrap the offending section in <text>tags. See this answer: https://stackoverflow.com/a/6099659/1451531

您需要将违规部分包装在<text>标签中。看到这个答案:https: //stackoverflow.com/a/6099659/1451531

回答by David Rettenbacher

I don't think you need to write the @in front of @dr[...]in the lines where you assign the value to LevelX

我不认为你需要在你分配值的行@前面写@dr[...]LevelX

<ul class="dropdown-menu">
@foreach (System.Data.DataRow dr in menu.Rows)
{
    if (Level1 != dr["Level1"].ToString())
    {
        <li><a href="#">@dr["Level1"].ToString()</a></li>
        Level1 = dr["Level1"].ToString(); // no need here

        if (Level2 != dr["Level2"].ToString())
        {
            <li><a href="#">@dr["Level2"].ToString()</a></li>
            Level2 = dr["Level2"].ToString();     // and no need here                                   
        } 
    }
} <!-- the issue is the bracket on this line -->
</ul>


EDIT

编辑

To address the question, why the closing bracket is not recognized.

为了解决这个问题,为什么不能识别右括号。

TL;DR
As you are again in code-region you do not need the @-sign.

TL;DR
当您再次处于代码区域时,您不需要@-sign

Details
The <li>...</li>section defines a html-region, where text is interpreted as html.
Directly after the closing </li>the outer code-region is active againand text is interpreted as C#-code. Therefore an assignment to Level1is possible. Adding an @-sign to drherehas the same consequences as in an "normal" cs-file:
It's a syntax-error.

详细信息
<li>...</li>部分定义了html-region,其中文本被解释为html
后直接封闭</li>-region是再次活性和文本被解释为C#-code。因此可以赋值给Level1。在此处添加 -@符号与dr“正常”cs 文件中具有相同的结果:
这是一个语法错误。

回答by Brett Donald

As per this answer, if a line is being interpreted as markup instead of code, put @ in front of it. If a line is being interpreted as code instead of markup, put @: in front of it.

根据这个答案,如果一行被解释为标记而不是代码,请将 @ 放在它前面。如果一行被解释为代码而不是标记,请将 @: 放在它前面。