2013-02-14 76 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