Click here to Skip to main content
15,890,185 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public class ClassRoom
{
    public string TeacherName { get; set; }
    public List<Student> StudentNames { get; set; }
}


I want to display the below mentioned ViewModel dynamic data to my MVC view (html table) in such a way that (only pseudo view)

Expected output:

Teacher1 Teacher2 Teacher3
----------------------------------------
Student1 Student5 Student9
Student2 Student6 Student10
Student3 Student7 Student11
Student4 Student8 Student12

For that purpose I have created the view as below,

HTML
@model IEnumerable<ClassRoom>
@{
    ViewBag.Title = "My ClassRoom";
}

<h2>My My ClassRoom</h2>

<table class="table">
    <tr>
        @foreach (var item in Model)
        {
            <th>
                @Html.DisplayFor(modelItem => item.TeacherName)
            </th>
            foreach (var name in item.StudentNames)
            {
            <td>
                @Html.DisplayFor(modelItem => name.FirstName)
            </td>
            }
        }
    </tr>
</table>


But the output looks different from my expected result,

Actual Output:

Teacher1
--------
Student1
Student2
Student3
Student4

Teacher2
--------
Student5
Student6
Student7
Student8

Teacher3
--------
Student9
Student10
Student11
Student12

Please help me to achieve my expected output

Note: The class room list is dynamic list from database table
Posted
Updated 20-Mar-15 23:27pm
v2

You loop through the items in the Model which are the students of one teacher, respectively. And in that loop you loop through the students. So It can only result in a list with one row.

To achieve your tabular presentation you have to output the first student of the first student-list, then the first student of the second student list, etc. Then in the next row, the second student of the first student list, then the second student of the second student list, etc.

One way to achieve that would be to create a list of student lists and then instead of using two foreach-loops (with the outer one iterating the student-lists and the inner one the students of one list) to use two for-loops with the outer one iterating the rows and the inner one iterating the student-lists.
If it's possible that student-lists have different amounts of students, you would have to check if the current index of the outer for-loop is valid for the current student-list (which wasn't neccessary before because of the foreach-loops).
 
Share this answer
 
Comments
Anoob Aliyar 24-Mar-15 7:16am    
Thanks manchanx. It is really helped me :)
[no name] 24-Mar-15 14:25pm    
Thank you for your feedback, I'm glad I could help you!
Hi, I believe manchanx explained nicely what problem you are facing and how to solve it, but nevertheless I will just add another suggestion that you can use.
You can create a main table (like a sort of a container), that would have a single row and in each of that row's cells you would have a nested table, so something like this:
HTML
@model IEnumerable<ClassRoom>
@{
    ViewBag.Title = "My ClassRoom";
}
 
<h2>My My ClassRoom</h2>
<table>
    <tr>
    @foreach (var item in Model)
    {
        <td>
            <table class="table">
                <tr>
                    <th>
                        @Html.DisplayFor(modelItem => item.TeacherName)
                    </th>
                </tr>
                @foreach (var name in item.StudentNames)
                {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => name.FirstName)
                    </td>
                </tr>
                }
            </table>
        </td>
    }
    </tr>
</table>
 
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