The following code enables multi-field sorting by clicking on the column headings to achieve sorting.
Controller:
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. (s = = S.lastname);
Break
Case "Lastname_desc":
Students = students. OrderByDescending (s = = S.lastname);
Break
Case "Date":
Students =students. (s =>s.enrollmentdate);
Break
Case "Date_desc":
Students = students. OrderByDescending (s = = s.enrollmentdate);
Break
Default
Students = students. (s = = S.firstmidname);
Break
}
Return View (students. ToList ());
}
View:
<table class= "Table Table-hover table-striped" >
<thead>
<tr>
<th>
@Html. ActionLink ("Last Name", "Index", new {SortOrder = Viewbag.firstnamesortparam})
</th>
<th>
@Html. ActionLink ("name", "Index", new {SortOrder = Viewbag.lastnamesortparm})
</th>
<th>
@Html. ActionLink ("Entry Time", "Index", new {SortOrder = Viewbag.datesortparm})
</th>
<th></th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{ ........................................................
ASP. NET MVC multi-field sorting