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

时间:2016-02-29 01:38:19

标签: c# asp.net asp.net-mvc entity-framework razor

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

╦═══════════╦
║ Season #  ║  <-------(SeasonID Selector DDL)
╩═══════════╩
╔═══╦═══════════╦═════════════════════════╗
║   ║ Team Name ║      Player Names       ║
╠═══╬═══════════╬═════════════════════════╣
║ 1 ║ GSW       ║ PlayerA PlayerB PlayerC ║
║   ║           ║ PlayerD Player E        ║
║ 2 ║ OKC       ║ PlayerA PlayerB PlayerC ║
║   ║           ║ PlayerD Player E        ║
║ 3 ║ CLE       ║ PlayerA PlayerB PlayerC ║
║   ║           ║ PlayerD Player E        ║
║ 4 ║ SAS       ║ PlayerA PlayerB PlayerC ║
║ 5 ║ TOR       ║ PlayerA PlayerB PlayerC ║
╚═══╩═══════════╩═════════════════════════╝

我设法通过将<form data-id="embedded_signup:form" class="ctct-custom-form Form" name="embedded_signup" method="POST" action="https://visitor2.constantcontact.com/api/signup"> <p>Sign up to get interesting news and updates delivered to your inbox.</p> <!-- The following code must be included to ensure your sign-up form works properly. --> <input data-id="ca:input" type="hidden" name="ca" value="my-secrect-key"> <input data-id="list:input" type="hidden" name="list" value="3"> <input data-id="source:input" type="hidden" name="source" value="EFD"> <input data-id="required:input" type="hidden" name="required" value="list,email"> <input data-id="url:input" type="hidden" name="url" value=""> <p data-id="Email Address:p" ><input data-id="Email Address:input" type="text" name="email" value="" maxlength="80"></p> <p data-id="First Name:p" ><input data-id="First Name:input" type="text" name="first_name" value="" maxlength="50"></p> <p data-id="Last Name:p" ><input data-id="Last Name:input" type="text" name="last_name" value="" maxlength="50"></p> <button type="submit" class="Button ctct-button Button--block Button-secondary" data-enabled="enabled">Sign Up</button> </form> 添加到SeasonTeam并循环浏览Viewbag list中的每个Team名称来实现此目的。然后,我会遍历Season模型中的每个项目,并将其与Roster中的SeasonTeamID进行比较,如果Viewbag list,则将玩家名称添加到行。我认为以下代码可以帮助解释。

实体之间的关系:Link

花名册模型:

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进行比较)球队名称,特别是当每个赛季将有大约100人参加比赛时。有替代方法吗?

1 个答案:

答案 0 :(得分:0)

最初的建议是改变这个:

var modelList = Model.ToList();

为此:

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

因此,modelList现在只会有你感兴趣的玩家,所以你不必迭代他们中的每一个,你就能够消除这个&#39;如果&#39;

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