c# - ASP.NET MVC - Adding multiple model items to a single column -
for mvc project, want add model elements table in following format:
╦═══════════╦ ║ 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 ║ ╚═══╩═══════════╩═════════════════════════╝
i managed adding seasonteam
viewbag list
, looping through each 1 team
name in season
. loop through each item in roster
model , compare seasonteamid
in viewbag list
, if viewbag list.seasonteamid == model.seasonteamid
, add player name row. think following code can explain.
relationship between entities: link
roster model:
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; } }
index:
@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 = 0; < modellist.count; i++) { if (modellist[i].seasonteamid == t.seasonteamid) { if (i % 3 == 0 && != 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> }
roster controller:
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()); }
the problem not want go through each , every player in model
(and compare seasonteamid
team list's seasonteamid
) whenever iterate through team name, when there around 100+ people playing per season. there alternative approach this?
an initial recommendation, change this:
var modellist = model.tolist();
for this:
var modellist = model.where(x => x.seasonteamid == t.seasonteamid).tolist();
therefore, modellist have players interested in, won't have iterate every single 1 of them, , able eliminate 'if':
if (modellist[i].seasonteamid == t.seasonteamid)
Comments
Post a Comment