FSou1
FSou1

Reputation: 1941

.NET three columns

I have IEnumerable<Class1> result (for example) in my View (ASP .NET MVC 3), and now i show it like two columns:

<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>

So, nice, but i have three columns on my HTML page. Is it possible with LINQ where to display my data for three columns? Dont know how to do it with simple and beautiful solution =\

<div id="left_column">    
</div>

<div id="center_column">
</div>

<div id="right_column">
</div>

My data is not fixed, so i dont know how much i could Skip and Take. Thx.

Upvotes: 1

Views: 176

Answers (2)

k.m
k.m

Reputation: 31454

Also consider using partial view (I'm using IEnumerable<PeopleTale> as model here, for better readability):

<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>

Your PeopleTalesList.cshtml partial view file will look like this:

@model IEnumerable<PeopleTale>
@foreach (var pt in Model)
{
<div class="item">
    <a href="#">@pt.NameAn</a>
</div>
}

Upvotes: 2

Maged Samaan
Maged Samaan

Reputation: 1752

you can do exactly what you are doing

<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>

i couldn't get something better

Upvotes: 1

Related Questions