我有IEnumerable<Class1> result
(例如)在我的视图(asp.net MVC 3),现在我显示它像两列:
<div id="left_column">
@foreach (PeopleTale pt in ((IEnumerable<PeopleTale>)ViewBag.PeopleTales).Where((x, i) => i % 2 == 1))
{
<div class="item">
<a href="#">@pt.NameAn</a>
</div>
}
</div>
<div id="center_column">
@foreach (PeopleTale pt in ((IEnumerable<PeopleTale>)ViewBag.PeopleTales).Where((x, i) => i % 2 == 0))
{
<div class="item">
<a href="#">@pt.NameAn</a>
</div>
}
</div>
所以,很好,但我有三个列在我的HTML页面。是否有可能与LINQ在哪里显示我的数据为三列?不知道如何用简单又漂亮的解决方案=
<div id="left_column">
</div>
<div id="center_column">
</div>
<div id="right_column">
</div>
我的数据不是固定的,所以我不知道我可以跳过多少。Thx .
还可以考虑使用局部视图(这里我使用IEnumerable<PeopleTale>
作为模型,为了更好的可读性):
<div id="left_column">
@Html.Partial("PeopleTalesList", Model.Where((pt, i) => i % 3 == 2))
</div>
<div id="center_column">
@Html.Partial("PeopleTalesList", Model.Where((pt, i) => i % 3 == 1))
</div>
<div id="right_column">
@Html.Partial("PeopleTalesList", Model.Where((pt, i) => i % 3 == 0))
</div>
你的PeopleTalesList.cshtml
部分视图文件看起来像这样:
@model IEnumerable<PeopleTale>
@foreach (var pt in Model)
{
<div class="item">
<a href="#">@pt.NameAn</a>
</div>
}
你可以做你正在做的事情
<div id="left_column">
@foreach (PeopleTale pt in ((IEnumerable<PeopleTale>)ViewBag.PeopleTales).Where((x, i) => i % 3== 2))
{
<div class="item">
<a href="#">@pt.NameAn</a>
</div>
}
</div>
<div id="center_column">
@foreach (PeopleTale pt in ((IEnumerable<PeopleTale>)ViewBag.PeopleTales).Where((x, i) => i % 3== 1))
{
<div class="item">
<a href="#">@pt.NameAn</a>
</div>
}
</div>
<div id="right_column">
@foreach (PeopleTale pt in ((IEnumerable<PeopleTale>)ViewBag.PeopleTales).Where((x, i) => i % 3== 0))
{
<div class="item">
<a href="#">@pt.NameAn</a>
</div>
}
</div>
我再也找不到更好的了