C# 将数据列表从控制器传递到视图
我正尝试使用以下代码按特定顺序检索数据(以建立排名):C# 将数据列表从控制器传递到视图,c#,asp.net-mvc,asp.net-mvc-4,razor,C#,Asp.net Mvc,Asp.net Mvc 4,Razor,我正尝试使用以下代码按特定顺序检索数据(以建立排名): public ActionResult ShowRanking(int id = 0) { Tournament tournament = db.Tournament.Find(id); var parti = (from p in db.Participe where p.IdTournament == tournament.
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);
}
数据会被检索,但每当我将数据列表传递给视图时,我使用的顺序标准都会被忽略,记录会显示为已插入数据库
以下是我的看法:
@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>
@model ArcheryComp.锦标赛
@{
ViewBag.Title=“Classement”;
}
等级
笔名
普勒农
分部
分类
得分1
得分2
总分
@foreach(Model.Participe中的var项)
{
@DisplayFor(modeleItem=>item.Archers.Nom)
@DisplayFor(modelItem=>item.Archers.Prenom)
@DisplayFor(modelItem=>item.Divisions.DivDescription)
@DisplayFor(modelItem=>item.Categorie)
@DisplayFor(modelItem=>item.ArchScore1)
@DisplayFor(modelItem=>item.ArchScore2)
@DisplayFor(modelItem=>item.ArchTotalScore)
var tmp=item.idp;
}
你知道怎么了吗?我能纠正一下吗
提前感谢您的帮助。您订购了
锦标赛。Participe
,但您没有使用结果。您必须将其更改为类似以下内容(如果锦标赛.Participe
是列表,当然可以):
视图中使用的模型是ArcheryComp.Tournament
,因此您必须返回view(Tournament)
而不是view(Tournament.Participe)
tournument.Particiate是什么?列表,哈希设置您需要将结果设置为OrderByDescending的结果并返回该结果。@dustmouse是正确的,OrderByDescending不修改原始列表,它只返回一个新的已订购版本。感谢您的反馈@dustmouse如何将我的结果设置为OrderByDescending的结果?@DavidE,你在使用实体框架吗?
tournament.Participe =
tournament.Participe.OrderByDescending(x => x.ArchTotalScore).ToList();
return View(tournament);