繁体   English   中英

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

[英]Passing list of data from controller to view

我正在尝试使用以下代码按特定顺序检索数据(以建立排名):

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>

你有什么主意吗? 我可以纠正这个吗?

在此先感谢您的帮助。

您订购了tournament.Participe ,但未使用结果。 您必须将其更改为以下内容(如果tournament.Participe是课程List ):

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM