[英]ASP.NET MVC How to create table structure dynamically in view from viewmodel collection of nested objects
我正在尝试显示一组用户选择的学期的所有课程,动态地保持格式,就像在循环时的预期屏幕截图中一样(这样学期只显示一次属于它的各个课程,以及课程名称(CourseA)对于属于该课程的各个部分仅显示一次):
预期(仅适用于当前选择的 1 个学期,对于更多选择的学期,应保持相同的显示模式)
我有一个列表,它是存储数据的 Model.SemesterSchedule。 这是我尝试过的但它不起作用,使用多个 foreach 循环时布局被破坏,而且我不确定三重 foreach 是否是一个好主意,请提出更好的替代方法:
我有以下结构:
class SemesterSchedule
{
public int SemesterID {get;set;}
public int SemesterName {get;set;}
public int TotalClassSections {get;set;}
public List<Course> Courses {get;set;}
}
class Course
{
public string CourseTitle {get;set}
public int ClassID {get;set;}
public List<ClassSection> Sections {get;set}
}
class ClassSection
{
public string SectionType {get;set;} // Lecture or Lab etc
public int ClassID {get;set;}
public string InstructorFirstName {get;set;}
--------------------------------------
public bool Monday {get;set;}
public TimeSpan MondayStart {get;set;}
public TimeSpan MondayEnd {get;set;}
------------------------------------------
}
@if (Model.SemestersSchedule != null && Model.SemestersSchedule.Count() > 0)
{
<table id="CourseScheduleTable">
<thead>
<tr>
<th>Term</th>
<th>Course</th>
<th>Section</th>
<th>Class#</th>
<th>Instructor Name</th>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>FRI</th>
<th>SAT</th>
</tr>
</thead>
<tbody>
@{
foreach (SemesterSchedule sc in Model.SemestersSchedule)
{
<tr>
<td rowspan="@sc.TotalSections">@sc.SemesterName</td>
</tr>
foreach (Course c in sc.Courses)
{
<tr>
<td rowspan="@c.Sections.Count()">@c.Course.CourseRunTitle</td>
</tr>
foreach (ClassSection s in c.Sections)
{
<tr>
<td>@s.SectionType</td>
<td>@s.Classroom</td>
<td>@c.Course.FirstName @c.Course.MiddleName @c.Course.LastName</td>
<td>@(s.Monday ? new DateTime(s.MondayStart.Ticks).ToString("HH:mm tt") + "-" + new DateTime(s.MondayEnd.Ticks).ToString("HH:mm tt") : "---")</td>
<td>@(s.Tuesday ? new DateTime(s.TuesdayStart.Ticks).ToString("HH:mm tt") + "-" + new DateTime(s.TuesdayEnd.Ticks).ToString("HH:mm tt") : "---")</td>
<td>@(s.Wednesday ? new DateTime(s.WednesdayStart.Ticks).ToString("HH:mm tt") + "-" + new DateTime(s.WednesdayEnd.Ticks).ToString("HH:mm tt") : "---")</td>
<td>@(s.Thursday ? new DateTime(s.ThursdayStart.Ticks).ToString("HH:mm tt") + "-" + new DateTime(s.ThursdayEnd.Ticks).ToString("HH:mm tt") : "---")</td>
<td>@(s.Friday ? new DateTime(s.FridayStart.Ticks).ToString("HH:mm tt") + "-" + new DateTime(s.FridayEnd.Ticks).ToString("HH:mm tt") : "---")</td>
<td>@(s.Saturday ? new DateTime(s.SaturdayStart.Ticks).ToString("HH:mm tt") + "-" + new DateTime(s.SaturdayEnd.Ticks).ToString("HH:mm tt") : "---")</td>
</tr>
}
}
}
}
</tbody>
当您使用带有rowspan="n"
属性的<td>
时,您不得在接下来的 n 行中使用另一个<td>
。
正确的 HTML 应该是:
<table class="blueTable">
<thead>
<tr>
<th>Term</th>
<th>Course</th>
<th>Section</th>
<th>Class</th>
<th>Instructor</th>
<th>MON</th>
<th>TUE</th>
<th>WED</th>
<th>THU</th>
<th>FRI</th>
<th>SAT</th>
</tr>
</thead>
<tbody>
<tr> <!-- 11 columns -->
<td rowspan="4">2019 Term Spring</td>
<td rowspan="2">CourseA</td>
<td>Lecture</td>
<td>219</td>
<td>John Doe</td>
<td>---</td>
<td>09:00</td>
<td>---</td>
<td>09:00</td>
<td>---</td>
<td>---</td>
</tr>
<tr> <!-- 9 columns -->
<td>Lab</td>
<td>2019</td>
<td>John Doe</td>
<td>---</td>
<td>11:00</td>
<td>---</td>
<td>---</td>
<td>---</td>
<td>---</td>
</tr>
<tr> <!-- 10 columns -->
<td>CourseB</td>
<td>Lecture</td>
<td>212</td>
<td>Jennifer Doe</td>
<td>09:00</td>
<td>---</td>
<td>11:00</td>
<td>---</td>
<td>---</td>
<td>---</td>
</tr>
<tr> <!-- 10 columns -->
<td>CourseC</td>
<td>Lecture</td>
<td>102</td>
<td>Jim B</td>
<td>---</td>
<td>---</td>
<td>---</td>
<td>---</td>
<td>---</td>
<td>13:00</td>
</tr>
</tbody>
</table>
如本Fiddle中所见。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.