0
之前使用单个IEnumerable值我有一个列出剧院的礼堂名称的视图。在For Each
@ModelType IEnumerable(Of dbtheatersinfo.TheaterAuditorium)
<h2>Theater Auditoriums</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(Function(model) model.Theater.TheaterName)
</th>
<th>
@Html.DisplayNameFor(Function(model) model.TheaterAuditoriumName)
</th>
</tr>
@For Each item In Model
@<tr>
<td>
@Html.DisplayFor(Function(modelItem) item.Theater.TheaterName)
</td>
<td>
@Html.DisplayFor(Function(modelItem) item.TheaterAuditoriumName)
</td>
</tr>
Next
</table>
这里列出的所有放映厅具有相同的TheaterName,所以我想显示TheaterName的标记中的第一个实例(并从列表中删除)。我试过了:
<h2>Auditoriums for @Html.DisplayFor(Function(model) model.Theater.TheaterName) </h2>
但是,这给了我,“'剧院'不是'IEnumerable(ofAcademicAuditorium)'的成员。”数据在那里;它显示在For Each循环中。我只是无法弄清楚如何在循环之前使用它。
您可以随时获得集合中的第一个项目 - “@ Model.First()。Theater.TheaterName' –
只要列表中至少有一个项目,就可以使用'First' 。如果可能没有项目,那么你需要使用'FirstOrDefault'并说明它可能是'Nothing'。您可以在VB 2015或更高版本中使用空传播,例如'model.FirstOrDefault?.Theater.TheaterName'。无论哪种方式,您应该在循环中使用'Model.Skip(1)',以便忽略第一项。 – jmcilhinney