David E
David E

Reputation: 47

Passing list of data from controller to view

I'm trying to retrieve data in a specific order (to establish a ranking) with the below code:

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);
        }

The data is retrieved but whenever I pass the list ofdata to my view, the order criterias I used are ignored and the records are shown as they have been inserted in the DB.

Here is my view as well:

@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>

Do you have any idea what is wrong ? and could I correct this ?

Thanks in advance for your help.

Upvotes: 1

Views: 99

Answers (1)

Taher  Rahgooy
Taher Rahgooy

Reputation: 6696

You ordered the tournament.Participe, but you did not use the result. You must change it to something like this(if tournament.Participe is a Listofcourse):

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

The model used in the view is ArcheryComp.Tournament therefore you must return View(tournament) not View(tournament.Participe).

Upvotes: 1

Related Questions