zoukankan      html  css  js  c++  java
  • asp.net mvc 多字段排序

    以下代码可实现多字段排序,通过点击列标题,实现排序。

    控制器:

    public ActionResult Index(string sortOrder)
    {
    ViewBag.FirstNameSortParam = string.IsNullOrEmpty(sortOrder) ? "FirstName_desc" : "";
    ViewBag.LastNameSortParam = sortOrder == "LastName" ? "LastName_desc" : "LastName";
    ViewBag.DateSortParam = sortOrder == "date" ? "date_desc" : "date";
    var students = from s in db.Students
    select s;

    switch (sortOrder)
    {
    case "FirstName_desc":
    students = students.OrderByDescending(s => s.FirstMidName);
    break;
    case "LastName":
    students = students.OrderBy(s => s.LastName);
    break;
    case "LastName_desc":
    students = students.OrderByDescending(s => s.LastName);
    break;
    case "date":
    students =students.OrderBy(s =>s.EnrollmentDate);
    break;
    case "date_desc":
    students = students.OrderByDescending(s => s.EnrollmentDate);
    break;
    default:
    students = students.OrderBy(s => s.FirstMidName);
    break;
    }

    return View(students.ToList());
    }

    视图:

    <table class="table table-hover table-striped">
    <thead>
    <tr>
    <th>
    @Html.ActionLink("名", "Index", new { sortOrder = ViewBag.LastNameSortParam })

    </th>
    <th>
    @Html.ActionLink("姓", "Index", new { sortOrder =ViewBag.FirstNameSortParam  })

    </th>
    <th>
    @Html.ActionLink("入学时间", "Index", new { sortOrder = ViewBag.DateSortPaarm })
    </th>
    <th></th>
    </tr>
    </thead>
    <tbody>
    @foreach (var item in Model)
    { ........................................................

  • 相关阅读:
    [ICPC2016上海F] Mr. Panda and Fantastic Beasts
    [ICPC2016上海E] Bet
    [ICPC2016上海D] Ice Cream Tower
    [ICPC2016上海L] World Cup
    [CCPC2020长春F] Strange Memory
    [CF1495C] Garden of the Sun
    【实战】牛年的第一个SQL注入漏洞
    【实战】一次有趣的HPP绕WAF记录
    [模板]二分答案
    字典树及其应用
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4942638.html
Copyright © 2011-2022 走看看