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

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

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 *@ 
+4
source share
1 answer

If the Amount collection is ordered correctly (I assume yes), you can simply use:

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

If they are in some other random order, you will need a way to sort them according to the column headings A, B, C, D ...

+3
source

Source: https://habr.com/ru/post/1412721/


All Articles