剃刀查看动态表中的行 [英] Razor View dynamic table rows
问题描述
我想在我的位子是打算把3个元素从我的模型中,每个一行。所以我要做到这一点的方式是通过模型环路和foreach循环里面做我已成立了一个计数变量的检查。如果数MOD 3 == 0我会做类似< / TR>< TR>
来开始新的一行。这不是工作,我希望它因为我会的方式}
继< TR>
。所以基本上我的问题是,我将如何创建基于在每排有3个项目模型中的元素剃刀视图内动态表?
I want have a table in my view that is going to put 3 elements from my Model in each row. So the way I was going to do this is to loop through the model and inside of the foreach loop do a check on a count variable I have set up. If count mod 3 == 0 I would do something like </tr><tr>
to start a new row. This isn't working the way I want it to because I would have a }
following the <tr>
. So basically my question is, how would I create a dynamic table inside of a razor view based on the elements in the model where each row has 3 items?
@{
int count = 0;
<div>
<table>
<tr>
@foreach (var drawing in Model)
{
<td style="width:240px;margin-left:30px; margin-right:30px;">
<img src="@Url.Action("GetImage", "Home", new { id = drawing.drw_ID })" alt="drawing" />
</td>
count++;
if(count%3==0)
{
</tr>
<tr>
}
}
</tr>
</table>
</div>
}
也许有这样做的,我没有想到的更简单的方法
Maybe there is a much easier way of doing this that I am not thinking of
推荐答案
如何使用两个循环 - 这会让你的文件进行设置更加精美,让更多的可读性。此外,它需要照顾如果行数是不能被3除尽出现的问题:
How about using two loops - this will make your document be setup much more nicely and make it a bit more readable. Also, it takes care of the problems that occur if the number of rows is not divisible by three:
<div>
<table>
@for(int i = 0; i <= (Model.Count - 1) / 3; ++i) {
<tr>
for(int j = 0; j < 3 && i + j < Model.Count; ++j) {
<td style="width:240px;margin-left:30px; margin-right:30px;">
<img src="@Url.Action("GetImage", "Home", new { id = Model[i + j].drw_ID })" alt="drawing" />
</td>
}
</tr>
}
</table>
</div>
编辑,以反映你的粘贴code。请注意,这个假设模型实现IList或阵列
这篇关于剃刀查看动态表中的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!