如何使用 Entity Framework 脚手架将一对多关系中的多边显示为 table

How to show the many side in a one-to-many relation as a table using Entity Framework scaffolding

选择球队后,我需要显示该球队的球员。

我认为更好的方法是在 create/edit/show 团队视图中显示 table 名玩家。

这是我的模型:

public class Player
{
    [Key]
    public int PlayerId { get; set; }
    [Required]
    [Display(Name = "Name", AutoGenerateField = false)]
    public string name { get; set; }
    public int TeamId { get; set; }
    [Display(Name = "Team", AutoGenerateField = false)]
    public virtual Team Team { get; set; }
}

public class Team
{
    [Key]
    public int TeamId { get; set; }
    [Required]
    [Display(Name = "Name", AutoGenerateField = false)]
    public string name { get; set; }
    [Display(Name = "Players")]
    public virtual ICollection<Player> Players { get; set; }
}

但是 entity framework 脚手架不会创建 table 内部团队视图。它只是创建一个组合框来在玩家视图中选择球队。

我在互联网上搜索过,但没有找到任何相关信息。我如何强制脚手架生成此 table?或者需要手动创建这个table?

我在 asp.net mvc 项目中使用 Visual Studio 2015 和 Entity Framework 6+ 和 C#。

我要展示的是这样的:

团队

姓名:幸存者

玩家

 _________
| Name  v 
 _________
| Bill    
| Louis   
| Zoey    
| Francis 
 _________

我能够通过手动编辑我的团队视图来显示 table。刚刚补充说:

 <table class="table">
    <tr>
        <th>
            Name
        </th>
        <th></th>
    </tr>


    @foreach (var player in Team.Players)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => player.name)
            </td>
            <td>
                @Html.ActionLink("Create New", "Create", "Players") |
                @Html.ActionLink("Edit", "Edit", new  { controller = "Players", id = player.PlayerId }) |
                @Html.ActionLink("Details", "Details", new  { controller = "Players", id = player.PlayerId }) |
                @Html.ActionLink("Delete", "Delete", new  { controller = "Players", id = player.PlayerId })
            </td>
        </tr>
    }

</table>

不是最简单的方法,但有效。