Click here to Skip to main content
15,910,277 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, i'm still new to the c# and need come help in displaying the data.

here's what i want:
HTML
<tr><th>Column 1 </th> <td> UnitName1</td>   <td>UnitName2 </td></tr>
<tr><th>Column 2</th>  <td> UnitName3</td>   <td>UnitName4 </td></tr>

i've listed all the unit names in the home controller like this:

private List<Units> Unit;
        public HomeController()
        {
            ViewBag.UnitName = Unit;

            Unit = new List<Units>()
            {
                new Units() {UnitName = "UnitName1"},
                new Units() {UnitName = "UnitName2"},
                new Units() {UnitName = "UnitName3"},
                new Units() {UnitName = "UnitName4"},
                new Units() {UnitName = "UnitName5"},
                new Units() {UnitName = "UnitName6"},
           }; 


i don't know how to display the data using the if statement because i want only 2 unit name in a row.

What I have tried:

this is the code in my cshtml that i've tried:

@foreach (var d in Model)
    {
        <tr>
            <td>d.UnitName</td>
        </tr>
    }


this code is true as it executes the unit name but in one column.
can i still use the "foreach" statement to achieve what i want? thankyou :)
Posted
Updated 14-Apr-20 3:47am
v2

No need for tuples; just use a nested for loop:
Razor
@for (int i = 0; i < Model.Count; i += 2)
{
    <tr>
        <th scope="row">...</th>
        @for (int j = i; j < 2 && j < Model.Count; j++)
        {
            <td>@Model[j].UnitName</td>
        }
    </tr>
}
 
Share this answer
 
Comments
Maciej Los 14-Apr-20 10:04am    
5ed!
That was my first thought. Finally, i decided to use Tuple.
Richard Deeming 14-Apr-20 10:08am    
My other thought was a tuple with a GroupBy:
IEnumerable<IGrouping<int, Unit>> groupedUnits = Units
    .Select((u, i) => Tuple.Create(i, u))
    .GroupBy(t => t.Item1 / 2, t => t.Item2);
@foreach (IGrouping<int, Unit> group in groupedUnits)
{
    <tr>
        <th>...</th>
        @foreach (Unit unit in group)
        {
            <td>@unit.UnitName</td>
        }
    </tr>
}
:)
Maciej Los 14-Apr-20 10:28am    
Very nice implementation. Looks much better than mine.
If you would like to display 2 units in one row, you have to create a Tuple<Unit, Unit>:

C#
var UnitPairs =  Units
    .Select((u, i) => Tuple.Create(i, u)).Where(x=>x.Item1%2==0)
    .Join(Units.Select((u, i)  => Tuple.Create(i, u)).Where(x=>x.Item1%2!=0),
        left => left.Item1 +1,
        right => right.Item1,
        (left, right) => new { Left = left, Right = right })
    .Select(x=> Tuple.Create(x.Left.Item2, x.Right.Item2));

then...
HTML
@foreach(Tuple<Unit, Unit> up in UnitPairs)
{
      <tr>
        <td>up.Item1.UnitName</td>
        <td>up.Item2.UnitName</td>
      <tr>
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900