从foreach循环生成表行
本文关键字:foreach 循环 | 更新日期: 2023-09-27 17:58:05
我已经在这个问题上呆了一段时间了;我将ASP.NET
、MVC 4
和C#
用于web应用程序。我从控制器中读取了一个Excel文件,并将所有单元格的List
发送回view
。这就是我正在使用的:
<table>
@foreach (var item in ViewBag.range)
{
<tr>
@for (int i = 0; i < 6; i++)
{
<td>
<input style="width:50px;" value=@item />
</td>
}
</tr>
}
</table>
基本上,我在Excel
中有6列。我正在尝试在我的view
中重新创建Excel
。但我的for loop
有问题,它每个细胞都做了6次。
有人能帮忙吗?
它这样做是因为您在for
循环中告诉它。也许你应该删除它。
<table>
<tr>
@foreach (var item in ViewBag.range)
{
<td>
<input style="width:50px;" value=@item />
</td>
}
</tr>
</table>
编辑
这将把range
内部的项放入行中,每行有6列。
@{
int total = 0;
}
<table>
@foreach (var item in ViewBag.range)
{
if( total % 6 == 0 ){
@:<tr>
}
<td>
<input style="width:50px;" value=@item />
</td>
if( total+1 % 7 == 0 ){
@:</tr>
}
total++;
}
</table>
由于我没有足够的范围信息。。。。我做了以下假设;修改并使用
@{var counter=0}
<table>
<tr>
@foreach (var item in ViewBag.range)
{
counter++;
<td>
<input style="width:50px;" value=@item />
</td>
if(counter%6==0)
{
@:</tr>
if(counter<ViewBag.range.Count)
{
@:<tr>
}
}
}
</table>