繁体   English   中英

ASP.NET MVC中带有Razor语法的嵌套代码

[英]Nested Code with Razor Syntax in ASP.NET MVC

我正在使用RAZOR的ASP.NET MVC 4应用程序上工作。 该应用程序基本上是带有组的记录列表。 为了尝试生成组标题和每个组的记录,我使用以下方法:

@{ string currentGroupName = string.Empty; }
@foreach (DataRow row in ViewBag.TableRows) {
  string groupName = Convert.ToString(row["Group"]); 
  if (groupName != currentGroupName) {
    <div style="width:98%;" class="nfo">@groupName</div>
    <table id="groupTable" border="0" class="t" style="margin-left:6px;">
      <thead>
        <tr>
          <th>Header 1</th>
          <th>Header 2</th>
          <th>Header 3</th>
        </tr>
      </thead>
      <tbody>
    }
                  ...
  @if (groupName != currentGroupName) {
      </tbody>
    </table>
    currentGroupName = groupName;
  }
}

执行此代码时,出现错误消息:

Parser Error
The foreach 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.

我究竟做错了什么?

您可以尝试使用@:运算符:

@{ string currentGroupName = string.Empty; }
@foreach (DataRow row in ViewBag.TableRows) 
{
    string groupName = Convert.ToString(row["Group"]); 
    if (groupName != currentGroupName) 
    {
        <div style="width:98%;" class="nfo">@groupName</div>
        @:<table id="groupTable" border="0" class="t" style="margin-left:6px;">
            <thead>
                <tr>
                    <th>Header 1</th>
                    <th>Header 2</th>
                    <th>Header 3</th>
                </tr>
            </thead>
            @:<tbody>
    }

    ...

    @if (groupName != currentGroupName) 
    {
            @:</tbody>
        @:</table>
        @{currentGroupName = groupName;}
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM