标签:
以下代码可实现多字段排序,通过点击列标题,实现排序。
控制器:
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)
{ ........................................................
标签:
原文地址:http://www.cnblogs.com/liuyuanhao/p/4942638.html