Paging and sorting grids with ASP.Net MVC

I am new to MVC and don’t understand how you will pager and sort by grid. I’m used to using the asp.Net GridView control with the ObjectDataSource object specified in our business layer objects β€” in which case ODS handles all the search calls and sorting using the methods that our ORM generates for the objects.

I looked at using the same ORM with MVC - and everything works fine there - I just loop through the collections to build the table on the page - but without ODS to handle swapping and sorting, I am confused about how I can handle this. Will I have a separate controller for swapping and sorting?

I understand that I need to collapse on my own, but where to start? I created a CustomerController and a view that displays a customer table that looks lower - and I want to sort the FirstName or LastName columns. My model has a Sort () method that will accept a string sort expression in the format that the GridView / ODS pair will use. Create a new action on my CustomerController called Sort and put the ActionLink in your header?

<table> <tr> <th> First Name </th> <th> Last Name </th> </tr> <% foreach (var item in Model) { %> <tr> <td> <%= Html.Encode(item.FirstName) %> </td> <td> <%= Html.Encode(item.LastName) %> </td> </tr> <% } %> </table> 
+52
asp.net-mvc gridview objectdatasource
Jan 30 '09 at 17:46
source share
5 answers

You can use the same controller, just add an additional parameter and name it sort. Then check in the controller what sorting of values ​​is and sort the data based on this parameter.

Or, if you want to do something on the client side, you can use something like tablesorter , a jquery plugin.

+29
Jan 30 '09 at 22:43
source share
β€” -

now MVC 3 now has a webGrid out of the box. I know this question was a long time ago, but I ended up on it when I was looking for something about webGrid. So I thought he should have an answer regarding the new webGrid.

Here are some good posts on how to use it:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=615

http://cnug.co.in/blogs/shijuv/archive/2010/10/08/using-the-webgrid-helper-in-asp-net-mvc-3-beta.aspx

http://www.nickharris.net/tag/webgrid/

It supports sorting, swapping, as well as some Ajax elements. It can do a lot for you already, but you can also specify each individual column separately.

Update:
There are also many JavaScript libraries available that can make a table for you. I personally like to use DataTables . You can give it an existing html table generated on the server, or provide it with an endpoint where it can retrieve data (all or just one page).

There are many more, just google around.

+38
Mar 18 '11 at 9:02
source share

With MVC, you need to minimize your own sorting, swap, etc. I would suggest a YUI DataTable or some other JavaScript grid there.

Also, if you find that your personal work is hard work with a data network, you can take a look at ASP.NET dynamic data, it is specially designed for these types of interactions with ORM.

+3
Jan 30 '09 at 17:58
source share

Use jQuery first. jQuery is your friend. Then use this amazing and probably best mesh control for jQuery jqGrid .

In your CustomerController, create a CustomerData action. All interaction with the grid should indicate this action.

Go here for a few examples on how to use jqGrid.

+2
Jan 30 '09 at 21:55
source share

We used JqxGrid on the client side from JqWidgets and are very pleased with its performance associated with a huge number of records, as well as swapping, filtering, sorting built-in inside. Here is an example of binding it in ASP.Net MVC

0
Feb 23 '15 at 11:59
source share



All Articles