将数据列表从控制器传递到视图
本文关键字:视图 控制器 数据 列表 | 更新日期: 2023-09-27 18:08:02
我试图检索数据在一个特定的顺序(建立一个排名)与以下代码:
public ActionResult ShowRanking(int id = 0)
{
Tournament tournament = db.Tournament.Find(id);
var parti = (from p in db.Participe
where p.IdTournament == tournament.IdTournament
//orderby p.ArchTotalScore descending
select p).OrderByDescending(x => x.ArchTotalScore);
//objlist = parti;
foreach (var part in parti)
{
tournament.Participe.Add(part);
//objlist.Add(part);
}
tournament.Participe.OrderByDescending(x => x.ArchTotalScore);
return View(tournament.Participe);
}
数据被检索,但是每当我将数据列表传递给我的视图时,我使用的顺序标准被忽略,并且记录显示为已插入到DB中的记录。
这也是我的观点:
@model ArcheryComp.Tournament
@{
ViewBag.Title = "Classement";
}
<h2>Classement</h2>
<table>
<tr>
<th>
Nom
</th>
<th>
Prenom
</th>
<th>
Division
</th>
<th>
Categorie
</th>
<th>
Score 1
</th>
<th>
Score 2
</th>
<th>
Total Score
</th>
</tr>
@foreach (var item in Model.Participe)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.Archers.Nom)
</td>
<td>
@Html.DisplayFor(modelItem => item.Archers.Prenom)
</td>
<td>
@Html.DisplayFor(modelItem => item.Divisions.DivDescription)
</td>
<td>
@Html.DisplayFor(modelItem => item.Categorie)
</td>
<td>
@Html.DisplayFor(modelItem => item.ArchScore1)
</td>
<td>
@Html.DisplayFor(modelItem => item.ArchScore2)
</td>
<td>
@Html.DisplayFor(modelItem => item.ArchTotalScore)
</td>
<br />
</tr>
var tmp = item.IdTournament;
}
</table>
你知道出了什么问题吗?我可以纠正这个吗?
提前感谢您的帮助。
您订购了tournament.Participe
,但没有使用结果。您必须将其更改为以下内容(当然,如果tournament.Participe
是List
):
tournament.Participe =
tournament.Participe.OrderByDescending(x => x.ArchTotalScore).ToList();
return View(tournament);
视图中使用的模型是ArcheryComp.Tournament
,因此您必须返回View(tournament)
而不是View(tournament.Participe)
。