ArchieTiger
ArchieTiger

Reputation: 2243

How can I arrange items in a table - MVC3 view (Index.cshtml)

I want to show the amount of different types of vitamins present in specific types of food samples, using ASP.NET MVC3. How can I display this in my View (Index.cshtml) ?

an example: enter image description here

And these are my codes:

<table>
<tr>
    <th></th>
    @foreach (var m in Model)
    {
         foreach (var v in m.Vitamins)
         {
              <th>@v.Name</th> 
         }           
    }
</tr>

   @foreach (var m in Model)
   {
         foreach (var f in m.Foods)
        {
             <tr>
                  <td>@f.Type</td>
             </tr> 
        }          
   }
</table>

@*The amount of Vitamins in each food:

       var a in m.AmountOfVitamins
       @a.Amount

*@

Upvotes: 4

Views: 7181

Answers (1)

McGarnagle
McGarnagle

Reputation: 102783

If the Amount collection is ordered correctly (I'm guessing yes), then you could use simply:

foreach (var f in m.Foods)
{
    <tr>
        <td>@f.Type</td>
        foreach (var a in m.AmountOfVitamins)
        {
            <td>a.Amount</td>
        }
    </tr> 
} 

If they're in some other random order, then you'd need a way to sort them according to the header columns A, B, C, D ...

Upvotes: 3

Related Questions