将数据列表从控制器传递到视图

时间:2015-08-14 14:44:11

标签: 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.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>

你知道出了什么问题吗?我可以纠正这个吗?

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您订购了tournament.Participe,但未使用结果。您必须将其更改为类似的内容(如果tournament.ParticipeList当然的话):

tournament.Participe = 
       tournament.Participe.OrderByDescending(x => x.ArchTotalScore).ToList();
return View(tournament);

视图中使用的模型为ArcheryComp.Tournament,因此您必须返回View(tournament)而不是View(tournament.Participe)

相关问题