【发布时间】:2014-11-30 21:33:48
【问题描述】:
我有一个 8 行的 ViewData。 ForEach 循环工作正常,但我需要提取 foreach 之前的第一行。
我只需要提取第一行以在 iframe 中注入默认视频。
<iframe name="myFrame" width="800" height="500" src="@item.ID?wmode=transparent" allowfullscreen="True"></iframe>
这是 100% 有效的 foreach
@foreach (var item in (List<VideoModel>)ViewData["Videos"])
{
<tr class="sep">
<td>@item.DisplayNumber</td>
<td>
@Html.ActionLink("Play Video", "IframeRedirect", "Home", new { ContentID = item.ID }, new { target = "someFrame", @class = "cbutton" })
</td>
<td>@item.Time @item.Hd</td>
<td><b>@item.Title</b><br />@item.Description</td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td><b>Author:</b> @item.Author <br /><a href="@item.AuthorSubscriptionUrl" target="@item.AuthorSubscriptionUrl">Subscribe to youtube channel</a></td>
</tr>
}
【问题讨论】:
-
@foreach (var item in (List<VideoModel>)ViewData["Videos"])??使用强类型模型将集合传递给您的视图!@model List<VideoModel>和@foreach(var item in Model) {。访问第一项Model[0] -
您正在查看视图代码。有没有更短的方法来显示第一行而不必使用@foreach 模型[0]?
-
不是
foreach model[0](你不需要循环来获取第一个项目)。如果您的模型是List<VideoModel>,那么Model[0]返回集合中的第一项(因此Model[0].ID返回集合中第一个VideoModel的ID 属性值。
标签: asp.net-mvc-4 razor