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)
    { ........................................................

  • 相关阅读:
    中间件的应用
    报表和日志
    Cookie和Session
    表单的应用
    静态资源和Ajax请求
    导入导出EXEC
    DATEDIFF() 函数返回两个日期之间的时间
    解决echarts柱形图X轴标题显示不全的问题
    Sqlserver 游标 慢
    sql语句根据日期查询,本周,本月,本年,今日相关统计
  • 原文地址:https://www.cnblogs.com/liuyuanhao/p/4942638.html
Copyright © 2011-2022 走看看