2013-02-14 34 views
0

我正在使用RAZOR的ASP.NET MVC 4应用程序。该应用程序基本上是一个包含组的记录列表。在试图生成一组标题,并与各组的记录,我使用了以下内容:在ASP.NET中使用Razor语法嵌套代码MVC

@{ 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. 

什么我做错了吗?

+0

就拿@关if语句。除非您之间有其他标记,否则您在此时仍处于代码模式。 – 2013-02-14 18:15:06

回答

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;} 
    } 
} 
+0

如果将'currentGroupName = groupName;'移动到右括号后面的行,则不需要围绕它的'@ {...}''。 – 2013-02-14 18:22:43

相关问题