我有一个视图,列出了剧院的礼堂名称。
@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>
但这给了我,&#34;&#39; Theatre&#39;并不是“剧院礼堂”(IE)的成员。&#39;。&#34;。数据在那里;它在For Each循环中显示。我只是无法弄清楚如何在循环之前使用它。
答案 0 :(得分:1)
我问了一个错误的问题,但无论如何最终都需要答案。为此,请正确查看,我需要一个视图模型:
Namespace ViewModels
Public Class AuditoriumIndex
Public Property TheaterAuditorium As IEnumerable(Of TheaterAuditorium)
Public Property Theater As Theater
End Class
End Namespace
在控制器中:
Dim viewModel = New AuditoriumIndex()
viewModel.TheaterAuditorium = db.TheaterAuditoriums.Where(Function(a) a.TheaterID = id).SortBy("TheaterAuditoriumName")
viewModel.Theater = db.Theaters.Where(Function(a) a.TheaterID = id).SingleOrDefault()
Return View(viewModel)
观点:
h2>Auditoriums for @Html.DisplayFor(Function(model) model.Theater.TheaterName)</h2>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(Function(model) model.TheaterAuditorium.FirstOrDefault().TheaterAuditoriumName)
</th>
</tr>
@For Each item In Model.TheaterAuditorium
@<tr>
<td>
@Html.DisplayFor(Function(modelItem) item.TheaterAuditoriumName)
</td>
</tr>
Next
</table>
这里我必须使用FirstOrDefault()来访问循环之外的列名。