访问剃刀页面中的列表需要使用 First()

问题描述

好的,所以我有我的视图模型

public  class Clubsviewmodel
{
     public List<Club> Clubs { get; set; }
     public List<ClubDownloads> ClubDownloads { get; set; }
}

这意味着可以是视图模型的许多俱乐部部分,但是当我转到我的 cshtml 文件时,我必须这样访问。

@model IEnumerable<MSFSAddons.Dal.viewmodels.Clubsviewmodel>

@foreach (var item in Model.First().Clubs)
{
    <tr>
        <td>
            @Html.displayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.displayFor(modelItem => item.Description)
        </td>
        <td style="background-color:gray">
            <img src="@item.logo" width="100" height="100" />
        </td>
        <td>
          <a asp-action="Index" asp-controller="ClubMembers" asp-route-id="@item.Id"> <span class="fa fa-users-cog fa-2x"></span> View Members</a>
        </td>

        <td>
            <a asp-action="Edit" asp-route-id="@item.Id"><span class="fa fa-pencil fa-2x"></span></a> |
            <a asp-action="Delete" asp-route-id="@item.Id"><span class="fa fa-trash-o fa-2x"></span></a> |

        </td>
    </tr>
}

如果我不能通过 Model.Clubs 访问它,为什么我必须使用 first() 我在这里做错了什么?

如果您在这里看到俱乐部是不可访问的。

enter image description here

解决方法

您的模型是 ClubsViewModel 的集合。将其更改为单个单元:

@model MSFSAddons.Dal.ViewModels.ClubsViewModel