Tri dans une application ASP.NET MVC

Pour commencer, ajoutons une fonctionnalité de tri par prix dans notre méthode SearchIndex située dans le fichier Controllers/LivreController.cs. Voici comment cela peut être réalisé : ```

public ActionResult SearchIndex(string Categorie, string rechercheTexte, string ordre) { var listCateg = new List(); var queryCateg = from d in db.Livres orderby d.Categorie select d.Categorie;

listCateg.AddRange(queryCateg.Distinct());
ViewBag.categorie = new SelectList(listCateg);

var livres = from m in db.Livres
             select m;

if (!String.IsNullOrEmpty(rechercheTexte))
{
    livres = livres.Where(s => s.Titre.Contains(rechercheTexte));
}

switch (ordre)
{
    case "prix_asc":
        livres = livres.OrderBy(p => p.Prix);
        break;
    case "prix_desc":
        livres = livres.OrderByDescending(p => p.Prix);
        break;
    default:
        break;
}

if (string.IsNullOrEmpty(Categorie))
    return View(livres);
else
{
    return View(livres.Where(x => x.Categorie == Categorie));
}

}


</div>Dans ce code, nous utilisons les méthodes `OrderBy` et `OrderByDescending` pour effectuer un tri croissant ou décroissant selon le prix. Interface utilisateur avec options de tri
-----------------------------------------

Nous allons maintenant enrichir notre interface utilisateur en ajoutant des options de tri. Voici la mise à jour du fichier `Views/Livre/SearchIndex.cshtml` : <div>```

@model IEnumerable<MvcApplication1.Models.Livre>

@{
    ViewBag.Title = "Recherche de livres";
}

<h2>Recherche de livres</h2>

@using (Html.BeginForm("SearchIndex", "livre", FormMethod.Get)) 
{   
    <p>
        Catégorie : @Html.DropDownList("categorie", "Toutes")
        Titre : @Html.TextBox("rechercheTexte") 
        Tri : @Html.DropDownList("ordre", "Aucun")
        <input type="submit" value="Chercher"></input>
    </p>
}

<table>
    <tr>
        <th>@Html.DisplayNameFor(model => model.Categorie)</th>
        <th>@Html.DisplayNameFor(model => model.Titre)</th>
        <th>@Html.DisplayNameFor(model => model.NombreExemplaires)</th>
        <th>@Html.DisplayNameFor(model => model.AuteurID)</th>
        <th>@Html.DisplayNameFor(model => model.Prix)</th>
        <th>@Html.DisplayNameFor(model => model.DatePublication)</th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>@Html.DisplayFor(modelItem => item.Categorie)</td>
        <td>@Html.DisplayFor(modelItem => item.Titre)</td>
        <td>@Html.DisplayFor(modelItem => item.NombreExemplaires)</td>
        <td>@Html.DisplayFor(modelItem => item.AuteurID)</td>
        <td>@Html.DisplayFor(modelItem => item.Prix)</td>
        <td>@Html.DisplayFor(modelItem => item.DatePublication)</td>
        <td>
            @Html.ActionLink("Modifier", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Détails", "Details", new { id=item.ID }) |
            @Html.ActionLink("Supprimer", "Delete", new { id=item.ID })
        </td>
    </tr>
}
</table>

Pour tester cette fonctionnalité, lancez votre application dans Visual Studio sans débogage. Accédez ensuite à la page de recherche des livres et utilisez les options de tri disponibles dans la liste déroulante. Vous devriez voir les résultats mis à jour en fonction du choix sélectionné.

Étiquettes: ASP.NET-MVC entity-framework CSharp

Publié le 4 août à 12h36