Paging & Sorting grids with ASP.Net MVC

Posted by Scott Ivey on Stack Overflow See other posts from Stack Overflow or by Scott Ivey
Published on 2009-01-30T17:46:37Z Indexed on 2010/05/08 18:58 UTC
Read the original article Hit count: 224

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>

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about ASP.NET