我正在尝试使用项列表动态创建表。该表必须始终有6行,但列数未定义。例如,它可以是75或167项。
这是我到目前为止所做的:
<table>
<tr>
@{
for (int i = 0; i < Model.mListCardSets.Count; i++)
{
if (i%6 == 0)
{
@:<tr>
}
<td>
@Html.HiddenFor(_item => _item.mListCardSets[i].Name)
@Html.HiddenFor(_item => _item.mListCardSets[i].Code)
@Html.CheckBoxFor(_item => _item.mListCardSets[i].IsChecked, new { @class = "checkGroup4", @id = Model.mListCardSets[i].Name })
@Html.DisplayFor(_item => _item.mListCardSets[i].Name)
@Html.Image("~\\Images\\CardSetRarity\\" + Model.mListCardSets[i].Code + "_R.jpeg", Model.mListCardSets[i].Name, new { @title = Model.mListCardSets[i].Name })
</td>
if (i%6 == 0)
{
@:</tr>
}
}
}
</tr>
</table>
我尝试了其他多种方法但没有成功。
答案 0 :(得分:0)
找到我的答案!
以下是解决方案:
<table id="cardSetTable" style="overflow-x: scroll;">
@{
var divfactor = (Model.mListCardSets.Count / 6) + (Model.mListCardSets.Count % 6);
int count = 0;
<tr>
@for (int i = 0; i < divfactor; i++)
{
<td>
@Html.HiddenFor(_item => _item.mListCardSets[i].Name)
@Html.HiddenFor(_item => _item.mListCardSets[i].Code)
@Html.CheckBoxFor(_item => _item.mListCardSets[i].IsChecked, new { @class = "checkGroup4", @id = Model.mListCardSets[i].Name })
@Html.DisplayFor(_item => _item.mListCardSets[i].Name)
@Html.Image("~\\Images\\CardSetRarity\\" + Model.mListCardSets[i].Code + "_R.jpeg", Model.mListCardSets[i].Name, new { @class = "nullify", @title = Model.mListCardSets[i].Name })
</td>
}
@{
count++;
}
</tr>
<tr>
@for (int i = 0; i < divfactor; i++)
{
<td>
@Html.HiddenFor(_item => _item.mListCardSets[i + (count * divfactor)].Name)
@Html.HiddenFor(_item => _item.mListCardSets[i + (count * divfactor)].Code)
@Html.CheckBoxFor(_item => _item.mListCardSets[i + (count * divfactor)].IsChecked, new { @class = "checkGroup4", @id = Model.mListCardSets[i + (count * divfactor)].Name })
@Html.DisplayFor(_item => _item.mListCardSets[i + (count * divfactor)].Name)
@Html.Image("~\\Images\\CardSetRarity\\" + Model.mListCardSets[i + (count * divfactor)].Code + "_R.jpeg", Model.mListCardSets[i + (count * divfactor)].Name, new {@class = "nullify", @title = Model.mListCardSets[i + (count * divfactor)].Name })
</td>
}
@{
count++;
}
</tr>
<tr>
@for (int i = 0; i < divfactor; i++)
{
<td>
@Html.HiddenFor(_item => _item.mListCardSets[i + (count * divfactor)].Name)
@Html.HiddenFor(_item => _item.mListCardSets[i + (count * divfactor)].Code)
@Html.CheckBoxFor(_item => _item.mListCardSets[i + (count * divfactor)].IsChecked, new { @class = "checkGroup4", @id = Model.mListCardSets[i + (count * divfactor)].Name })
@Html.DisplayFor(_item => _item.mListCardSets[i + (count * divfactor)].Name)
@Html.Image("~\\Images\\CardSetRarity\\" + Model.mListCardSets[i + (count * divfactor)].Code + "_R.jpeg", Model.mListCardSets[i + (count * divfactor)].Name, new { @class = "nullify", @title = Model.mListCardSets[i + (count * divfactor)].Name })
</td>
}
@{
count++;
}
</tr>
<tr>
@for (int i = 0; i < divfactor; i++)
{
<td>
@Html.HiddenFor(_item => _item.mListCardSets[i + (count * divfactor)].Name)
@Html.HiddenFor(_item => _item.mListCardSets[i + (count * divfactor)].Code)
@Html.CheckBoxFor(_item => _item.mListCardSets[i + (count * divfactor)].IsChecked, new { @class = "checkGroup4", @id = Model.mListCardSets[i + (count * divfactor)].Name })
@Html.DisplayFor(_item => _item.mListCardSets[i + (count * divfactor)].Name)
@Html.Image("~\\Images\\CardSetRarity\\" + Model.mListCardSets[i + (count * divfactor)].Code + "_R.jpeg", Model.mListCardSets[i + (count * divfactor)].Name, new { @class = "nullify", @title = Model.mListCardSets[i + (count * divfactor)].Name })
</td>
}
@{
count++;
}
</tr>
<tr>
@for (int i = 0; i < divfactor; i++)
{
<td>
@Html.HiddenFor(_item => _item.mListCardSets[i + (count * divfactor)].Name)
@Html.HiddenFor(_item => _item.mListCardSets[i + (count * divfactor)].Code)
@Html.CheckBoxFor(_item => _item.mListCardSets[i + (count * divfactor)].IsChecked, new { @class = "checkGroup4", @id = Model.mListCardSets[i + (count * divfactor)].Name })
@Html.DisplayFor(_item => _item.mListCardSets[i + (count * divfactor)].Name)
@Html.Image("~\\Images\\CardSetRarity\\" + Model.mListCardSets[i + (count * divfactor)].Code + "_R.jpeg", Model.mListCardSets[i + (count * divfactor)].Name, new { @class = "nullify", @title = Model.mListCardSets[i + (count * divfactor)].Name })
</td>
}
@{
count++;
}
</tr>
<tr>
@{
var lastRow = divfactor*count;
for (int i = lastRow; i < Model.mListCardSets.Count; i++)
{
<td>
@Html.HiddenFor(_item => _item.mListCardSets[i].Name)
@Html.HiddenFor(_item => _item.mListCardSets[i].Code)
@Html.CheckBoxFor(_item => _item.mListCardSets[i].IsChecked, new { @class = "checkGroup4", @id = Model.mListCardSets[i].Name })
@Html.DisplayFor(_item => _item.mListCardSets[i].Name)
@Html.Image("~\\Images\\CardSetRarity\\" + Model.mListCardSets[i].Code + "_R.jpeg", Model.mListCardSets[i].Name, new { @class = "nullify", @title = Model.mListCardSets[i].Name })
</td>
}
}
</tr>
}
</table>