#html #razor
#HTML #бритва
Вопрос:
Я пытаюсь динамически создать таблицу со списком элементов. В таблице всегда должно быть 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>
И я пробовал множество других способов, но безуспешно.
Ответ №1:
Нашел мой ответ!
Вот решение:
<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>