Sorting a Grid of Data in ASP.NET MVC
Last week's article, Displaying a Grid of Data in ASP.NET MVC, showed, step-by-step, how to display a
grid of data in an ASP.NET MVC application. Last week's article started with creating a new ASP.NET MVC application in Visual Studio,
then added the Northwind database to the project and showed how to use Microsoft's Linq-to-SQL tool to access data from the database. The article then looked at creating
a Controller and View for displaying a list of product information (the Model).
This article builds on the demo application created in Displaying a Grid of Data in ASP.NET MVC, enhancing the grid to include bi-directional sorting.
If you come from an ASP.NET WebForms background, you know that the GridView control makes implementing sorting as easy as ticking a checkbox. Unfortunately, implementing
sorting in ASP.NET MVC involves a bit more work than simply checking a checkbox, but the quantity of work isn't significantly greater and with ASP.NET MVC we have
more control over the grid and sorting interface's layout and markup, as well as the mechanism through which sorting is implemented. With the GridView control, sorting
is handled through form postbacks with the sorting parameters - what column to sort by and whether to sort in ascending or descending order - being submitted as hidden
form fields. In this article we'll use querystring parameters to indicate the sorting parameters, which means a particular sort order can be indexed by search engines,
bookmarked, emailed to a colleague, and so on - things that are not possible with the GridView's built-in sorting capabilities.
Like with its predecessor, this article offers step-by-step instructions and includes a complete, working demo available for download at the end of the article.
Read on to learn more!
Read More >