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

asp.net, asp.net-mvc, asp.net-mvc-3, c#, razor

Solution

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

Problem

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: 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 *@ ```

Original source