C# 如何在MVC4.0视图页面中打印此内容
我有一个班的学生:C# 如何在MVC4.0视图页面中打印此内容,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我有一个班的学生: 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<st
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;}
}
我已经将数据放在ViewBag中,但我无法弄清楚如何使用foreach语句在下面的视图中打印所有这些记录。
视图:
学名
课程
职员
手机号码
@foreach(ViewBag.students中的学生钉)
{
@student.StudentName
foreach(ViewBag.courses中的课程)
{
@course.CourseName
}
foreach(ViewBag.Staff中的职员)
{
@职员姓名
}
}
但是,这会打印出所有学生为单个学生所选的课程,因为他们在第一个foreach循环中。请,给我一个方法来做这个 您不需要其他的可视包。在学生对象中有对课程和员工的引用
<table class="tableStyle" width="100%" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th>Student Name</th>
<th>Courses - Staffs</th>
<th>Mobile Numbers</th>
</tr>
</thead>
@foreach (Student stud in Viewbag.students)
{
<tr>
<td>@stud.StudentName</td>
<td>
foreach(Course course in stud.courses)
{
foreach(Staff staff in course.staff)
{
@course.CourseName - @staff.StaffName </br>
}
}
</td>
</tr>
}
</table>
学名
课程-员工
手机号码
@foreach(Viewbag.students中的学生钉)
{
@student.StudentName
foreach(预科课程)
{
foreach(课程中的员工。员工)
{
@course.CourseName-@staff.StaffName
}
}
}
您还可以考虑通过添加:
使IQueLead为您的页面模型。@model IEnumerable<myproject.Models.Student>
@model IEnumerable
到页面顶部。那么您就不需要使用Viewbag了。您使用的是实体框架吗?@DaleMarshall:是的,我使用的是实体框架。
<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>
<table class="tableStyle" width="100%" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th>Student Name</th>
<th>Courses - Staffs</th>
<th>Mobile Numbers</th>
</tr>
</thead>
@foreach (Student stud in Viewbag.students)
{
<tr>
<td>@stud.StudentName</td>
<td>
foreach(Course course in stud.courses)
{
foreach(Staff staff in course.staff)
{
@course.CourseName - @staff.StaffName </br>
}
}
</td>
</tr>
}
</table>
@model IEnumerable<myproject.Models.Student>