Asp.net mvc 对于每个变量,在之前使用单个IEnumerable值

Asp.net mvc 对于每个变量,在之前使用单个IEnumerable值,asp.net-mvc,vb.net,razor,Asp.net Mvc,Vb.net,Razor,我有一个视图列出了剧院的礼堂名称 @ModelType IEnumerable(Of dbtheatersinfo.TheaterAuditorium) <h2>Theater Auditoriums</h2> <table class="table"> <tr> <th> @Html.DisplayNameFor(Function(model) model.Theater.Theater

我有一个视图列出了剧院的礼堂名称

@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>
@ModelType IEnumerable(属于dbtheatersinfo.TheateAuditorium)
剧院礼堂
@Html.DisplayNameFor(函数(模型)model.Theater.TheaterName)
@Html.DisplayNameFor(函数(模型)模型.TheateAuditoriumName)
@对于模型中的每个项目
@
@Html.DisplayFor(函数(modelItem)项.Theater.TheaterName)
@Html.DisplayFor(函数(modelItem)项.TheateAuditoriumName)
下一个
这里列出的所有观众厅都有相同的TheateName,因此我想在标记中显示TheateName的第一个实例(并将其从列表中删除)。我试过:

<h2>Auditoriums for @Html.DisplayFor(Function(model) model.Theater.TheaterName) </h2>
Auditoriums for@Html.DisplayFor(函数(模型)model.Theater.TheaterName)

但这给了我,“‘剧院’不是‘IEnumerable(TheaterAuditorium)’的成员。”数据就在那里;它在For Each循环中显示。在循环之前,我不知道如何使用它。

我问错了问题,但最终还是需要答案。要正确执行此视图,我需要一个视图模型:

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>
h2>Auditoriums for@Html.DisplayFor(函数(模型)model.Theater.TheaterName)
@(函数(模型)模型.TheateAuditorium.FirstOrDefault().TheateAuditoriumName)的Html.DisplayNameFor
@对于Model.TheaterAuditorium中的每个项目
@
@Html.DisplayFor(函数(modelItem)项.TheateAuditoriumName)
下一个

在这里,我必须使用FirstOrDefault()来访问循环之外的列名。

您始终可以只获取集合中的第一项-
@Model.first().Theater。tername
您可以按照建议使用
first
,只要列表中肯定至少有一项。如果可能没有项目,则需要使用
FirstOrDefault
,并说明它可能是
Nothing
。您可以在VB 2015或更高版本中使用空传播,例如
model.FirstOrDefault?.Theater.theaternem
。无论哪种方式,您都应该在循环中使用
Model.Skip(1)
,以便忽略第一项。