For solution of above problem, we are creating one student controller and its index view. We want to display a sort records(i.e. First Name,Last Name or Enrollment date) in index view.
this is a code of Student Controller Index Action:
publicActionResultIndex(string sortOrder){ViewBag.NameSortParm=String.IsNullOrEmpty(sortOrder)?"Name":"";ViewBag.DateSortParm= sortOrder =="Date"?"Date_desc":"Date";var students =from s in db.Student // Student is a table i.e. containing records of studentselect s;switch(sortOrder){case"Name":
students = students.OrderByDescending(s => s.FirstName);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.FirstName);break;}returnView(students.ToList());}
In Views\Student\Index.cshtml, We have changed below lines:
<p>
@Html.ActionLink("Create New Student Record", "Create")
</p><table><tr><th>
@Html.ActionLink("First Name", "Index", new { sortOrder = ViewBag.NameSortParm })
</th><th> Last Name
</th><th>
@Html.ActionLink("Enrollment Date", "Index", new { sortOrder = ViewBag.DateSortParm })
</th><th></th></tr>
@foreach (var item in Model)
{
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Views\Student\Index.cshtml, We have changed below lines: