我有一个班学生:
public class Student
{
public int StudentId{get;set;}
public string StudentName{get;set;}
public int Age{get;set;}
public List<Course> Courses{get;set;}
public List<string> MobileNumbers{get;set;}
}
课程类是:
public class Course
{
public int CourseId {get;set;}
public string CourseName {get;set;}
public List<Staff> Staff {get;set;}
}
Staff 类具有以下结构:
public class Staff
{
public int StaffId {get;set;}
public string StaffName {get;set;}
}
我已将数据放入 ViewBag ,但我无法弄清楚我应该如何使用 foreach 语句在以下视图中打印所有这些记录。看法:
<table class="tableStyle" width="100%" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th>Student Name</th>
<th>Courses</th>
<th>Staffs</th>
<th>Mobile Numbers</th>
</tr>
</thead>
@foreach (Student stud in ViewBag.students)
{
<tr>
<td>@stud.StudentName</td>
foreach(Course course in ViewBag.courses)
{
<td>@course.CourseName</td>
}
foreach(Staff staff in ViewBag.staff)
{
<td>@staff.StaffName</td>
}
</tr>
}
</table>
但是,这会打印所有学生为单个学生学习的课程,因为他们在第一个 foreach 循环中。请给我建议一种方法来做到这一点......!