0

我正在开发一个使用 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.

我究竟做错了什么?

4

1 回答 1

0

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

@{ 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;}
    }
}
于 2013-02-14T18:10:20.257 回答