我的ASP.NET代码中有一个foreach
循环,我试图通过在循环结束时添加@counter++
来添加计数器,但是它不起作用,我得到一个无效的表达式术语错误。我也尝试了@{ counter++ }
,但这也没有用。
<table border="1" style="width:762px;height:25px; border-style:1px solid #000; border-collapse:collapse; clear:both;">
@{
int counter = 1;
}
@foreach (var person in @ViewBag.POLoopList)
{
<tr>
<td style="width:24px; text-align:right;">@counter</td>
<td style="width:55px">@person.EstPhase</td>
<td style="width:32px">@person.JCCategory</td>
<td style="width:180px">@person.ItemsDesc</td>
<td style="width:90px; text-align:left;">@person.Comments</td>
<td style="width:57px; text-align:right;">@person.OrderQty</td>
<td style="width:40px; text-align:center;">@person.OrderUOM</td>
<td style="width:56px; text-align:right;">@Convert.ToDouble(person.Rate).ToString("N")</td>
<td style="width:70px; text-align:right;">$@Convert.ToDouble(person.Pretax).ToString("N")</td>
</tr>
@counter++
}
</table>
答案 0 :(得分:3)
从@
:
@counter++
<table border="1" style="width:762px;height:25px; border-style:1px solid #000; border-collapse:collapse; clear:both;">
@{
int counter = 1;
}
@foreach (var person in @ViewBag.POLoopList)
{
<tr>
<td style="width:24px; text-align:right;">@counter</td>
<td style="width:55px">@person.EstPhase</td>
<td style="width:32px">@person.JCCategory</td>
<td style="width:180px">@person.ItemsDesc</td>
<td style="width:90px; text-align:left;">@person.Comments</td>
<td style="width:57px; text-align:right;">@person.OrderQty</td>
<td style="width:40px; text-align:center;">@person.OrderUOM</td>
<td style="width:56px; text-align:right;">@Convert.ToDouble(person.Rate).ToString("N")</td>
<td style="width:70px; text-align:right;">$@Convert.ToDouble(person.Pretax).ToString("N")</td>
</tr>
counter++
}
</table>
答案 1 :(得分:2)