ASP.NET MVC - 将多个模型项添加到单个列

本文关键字:模型 添加 单个列 NET MVC ASP | 更新日期: 2023-09-27 18:36:42

对于我的MVC项目,我想以以下格式将模型元素添加到表中:

╦═══════════╦║ 赛季 # ║ &lt-------(赛季 ID 选择器 DDL)╩═══════════╩╔═══╦═══════════╦═════════════════════════╗║ ║ 球队名称 ║ 球员姓名 ║╠═══╬═══════════╬═════════════════════════╣║ 1 ║ GSW ║ 玩家A 玩家B 玩家C ║║ ║ ║ 玩家 D 玩家 E ║║ 2 ║ OKC ║ 玩家 A 玩家B 玩家C ║║ ║ ║ 玩家 D 玩家 E ║║ 3 ║ CLE ║ 玩家A 玩家B 玩家C ║║ ║ ║ 玩家 D 玩家 E ║║ 4 ║ SAS ║ 玩家A 玩家B 玩家C ║║ 5 ║ TOR ║ 玩家A 玩家B 玩家C ║╚═══╩═══════════╩═════════════════════════╝

我设法通过将SeasonTeam添加到Viewbag list中并在Season中循环遍历每个Team名称来做到这一点。然后我会遍历Roster模型中的每个项目,并将其与Viewbag list中的SeasonTeamID进行比较,所以如果Viewbag list.seasonTeamID == model.SeasonTeamID,则将玩家名称添加到行中。我认为以下代码可以帮助解释。

实体之间的关系:链接

名册模型:

public partial class Roster
{
    public int RosterID { get; set; }
    public int SeasonTeamID { get; set; }
    public int PlayerID { get; set; }
    public virtual Player Player { get; set; }
    public virtual SeasonTeam SeasonTeam { get; set; }
}

指数:

@using (Html.BeginForm())
{
    <p>
        @Html.DropDownList("seasonID", (SelectList)ViewBag.SeasonList, new { onchange = "this.form.submit();" })
    </p>
}
<table class="table"> 
    <tr>
        <th></th>
        <th>
            @Html.DisplayNameFor(model => model.SeasonTeamID)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.SeasonTeam.Team.TeamName)
        </th>
        <th>
            @Html.DisplayName("Player List")
        </th>
        <th></th>
    </tr>
@{ 
    int rowCount = 0;
    var team = (List<SeasonTeam>)ViewBag.teamList;
}
@foreach (var t in team)
{
    <tr>
        <td>@(rowCount += 1)</td>
        <td>
            @Html.DisplayFor(modelItem => t.SeasonTeamID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => t.Team.TeamName)
        </td>
        <td>
            @* Loop through each player in roster *@
            @{ var modelList = Model.ToList();}
            <div class="row">
                @for (int i = 0; i < modelList.Count; i++)
                {
                    if (modelList[i].SeasonTeamID == t.SeasonTeamID)
                    {
                        if (i % 3 == 0 && i != 0)
                        {
                            @:</div><div class="row"> 
                            <div class="col-md-3">@modelList[i].Player.LastName</div>
                        }
                        else
                        {
                            <div class="col-md-3">@modelList[i].Player.LastName</div>
                        }                        
                    }                    
                }
            </div>
            @*End loop*@
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id = t.SeasonTeamID }) |
            @Html.ActionLink("Details", "Details", new { id = t.SeasonTeamID })
        </td>       
    </tr>
}

名册控制器:

    public ActionResult Index(int? seasonID)
    {
        var rosters = db.Rosters.Include(r => r.SeasonTeam).Where(r => r.SeasonTeam.SeasonID == seasonID).OrderBy(t => t.SeasonTeam.Team.TeamName);

        var sList = db.Seasons.OrderByDescending(a => a.SeasonID).ToList()
            .Select(s => new
            {
                SeasonID = s.SeasonID,
                Desc = "Season " + s.SeasonID
            });
        ViewBag.SeasonList = new SelectList(sList, "SeasonID", "Desc", seasonID);
        var steam = db.SeasonTeams.Include(r => r.Team).Where(r => r.SeasonID == seasonID).OrderBy(r => r.Team.TeamName);
        ViewBag.teamList = steam.ToList();
        return View(rosters.ToList());
    }

问题是,每当我迭代球队名称时,我都不想浏览model中的每个球员(并将其SeasonTeamID与球队列表的SeasonTeamID进行比较),尤其是当每个赛季大约有 100+ 人参加比赛时。有没有替代方法?

ASP.NET MVC - 将多个模型项添加到单个列

最初的建议是更改以下内容:

var modelList = Model.ToList();

为此:

var modelList = Model.Where(x => x.SeasonTeamID == t.SeasonTeamID).ToList();

因此,modelList 现在将只有您感兴趣的玩家,因此您不必迭代它们中的每一个,您将能够消除这个"如果":

if (modelList[i].SeasonTeamID == t.SeasonTeamID)