I'm working on an ASP.NET MVC 4 app that uses RAZOR. The app is basically a list of records with groups. In an attempt to generate a group heading and the records with each group, I'm using the following:
@{ 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;
}
}
When I execute this code, I get an error that says:
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.
What am I doing wrong?
You could try using the @:
operator:
@{ 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;}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.