码迷,mamicode.com
首页 > 编程语言 > 详细

asp.net mvc 多字段排序

时间:2015-11-06 16:01:01      阅读:268      评论:0      收藏:0      [点我收藏+]

标签:

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

控制器:

public ActionResult Index(string sortOrder)
{
ViewBag.FirstNameSortParam = string.IsNullOrEmpty(sortOrder) ? "FirstName_desc" : "";
ViewBag.LastNameSortParm = sortOrder == "LastName" ? "LastName_desc" : "LastName";
ViewBag.DateSortParm = 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.FirstNameSortParam })
</th>
<th>
@Html.ActionLink("名", "Index", new { sortOrder = ViewBag.LastNameSortParm })
</th>
<th>
@Html.ActionLink("入学时间", "Index", new { sortOrder = ViewBag.DateSortParm })
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{ ........................................................

asp.net mvc 多字段排序

标签:

原文地址:http://www.cnblogs.com/liuyuanhao/p/4942638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!