views:

9680

answers:

8

I'm new to MVC, and am not following how you'd do paging and sorting on a grid. I'm used to using the asp.Net GridView control with an ObjectDataSource pointed at objects in our business layer - and in that case the ODS handles all of the paging & sorting using the methods that our ORM generates on the objects.

I've looked at using the same ORM with MVC - and things work out fine there - i just loop thru the collections to build the table on the page - but without the ODS to handle the paging & sorting, i'm confused as to how I'd handle that. Would I have a separate controller for the paging and sorting? I'm not sure what the best practices are for this scenario, so if someone can point me in the right direction it would be much appreciated.

Edit:

Ok, so I understand that I need to roll my own - but where do I start? I've created a CustomerController, and a view that displays a table of customers that looks like below - and I want to sort on FirstName or LastName columns. My Model has a Sort() method on it that'll take a string sort expression in the format that would be used by a GridView/ODS pair. Would I create a new Action on my CustomerController called Sort, and put an ActionLink in my 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>
+2  A: 

Hi Scott,

With MVC you sort of have to roll your own sorting, paging, etc. I would suggest YUI DataTable or some of the other JavaScript Grids out there.

Also if you find your self doing heavy Data Grid work you may want to take a look at ASP.NET Dynamic Data, it is specifically designed for these types of interactions against ORM's.

Nick Berardi
I am going the route of MVC/Dynamic Data hybrid applications at the moment: the mvc stuff for the non-admin pages and the DD for admin pages.
Merritt
+5  A: 

There are extensions and HTML Helpers available for this but yes alot of it is "roll-your-own".

Here a an example...

http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/

[http://blogs.taiga.nl/martijn/archive/2008/08/27/paging-with-asp.net-mvc.aspx]

Brian Behm
link doesn't exist :)
ajbeaven
Looks like his blog changed to WordPress. Here's the link...http://blogs.taiga.nl/martijn/2008/08/27/paging-with-aspnet-mvc/
Brian Behm
A: 

First use jQuery. jQuery is your friend. Then use this awesome and probably the best Grid control for jQuery jqGrid.

In your CustomerController create an action called CustomerData. All interaction with the Grid should point to this action.

Go here for tons of examples on how to use jqGrid.

Donny V.
jqGrid is javascript dependent. What if you need JavaScript independency?
Andrei Rinea
Donny V.
+13  A: 

Your can use the same controller, just add an additional parameter and name it sort. Then check in the controller what value sort has, and sort your data based on that parameter.

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

Morph
A: 

Hey i used Table sorter to do the Sorting. In sorting the CSS Class also gets sorted. I dont want paging and isnteed of that i need Freezed Headers for the Table. Any idea how to get started?

Thanks in advance. Regards Aditya Marla

Aditya
A: 

i have found an example using DataTable and JQgrid at

http://arahuman.blogspot.com/2009/06/jqgrid-using-mvc-json-and-datatable.html

+1  A: 

Hi,

This seems to be quite a nice solution:

http://xlib.wordpress.com/2009/06/29/asp-net-mvc-grid-%E2%80%93-part-2-paging/

DavidGouge
+1  A: 

I have an example that using the MvcContrib Grid and Pager here:

http://weblogs.asp.net/rajbk/archive/2010/05/08/asp-net-mvc-paging-sorting-filtering-using-the-mvccontrib-grid-and-pager.aspx

Raj Kaimal