Show values in a nested loop horizontally in a table

I have two array.

TempData[“item_list”] like this => ["car1","car2","car3"]

Model like this => [{"item1","item3","item5"}, {"item2","item4"}, {"item4","item5"}]

    <table style="width:100%">
            <tr>
                @foreach (var car in TempData["item_list"] as IEnumerable<string>)
                {
                    <th>@car</th>
                }

                @foreach (var item in Model)
                {
                    @foreach (var itemm in item)
                    {

                        <td>@itemm</td>
                    }
                }
            </tr>
    </table>

That’s how I want to show it in the table.
Show values in a nested loop horizontally in a table

My View

Show values in a nested loop horizontally in a table

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

pseudocode for what you want –

var carArray = ["car1","car2","car3"];
var itemsArray = [{"item1","item3","item5"}, {"item2","item4"}, {"item4","item5"}];

for(int i = 0, i < carArray.Count, i ++)
{
   <tr>
      <td>
          <strong>@carArray[i]</strong>
      </td>

      @foreach(var item in itemsArray[i])
      {
          <td>itemArray[i]</td>              
      }
   </tr>
}

Each car is on a new row. Then loop through the items in the itemArray at the same index as the car and put it on the same row. This doesn’t contain any validation to verify the itemsArray length is the same as carArray, of course.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x