web 2.0

Looking for an MVC Grid Control? Try MVC Contrib!

Like most .NET Web Developers I was ecstatic when MVC was released. To put it plainly, I hate WebForms. However I do find myself missing some of the great WebForm controls like the DataGridView. The DataGridView was present in every WebForm application that I wrote. I really appreciated all the subtle bells and whistles that Microsoft added to the grid over the years. I wrote my own grid control for classic ASP and I know firsthand that it is a significant undertaking to make a grid control that if feature rich and flexible enough to handle complex situations. Therefore I was not crazy about taking on the task again…

Initially, I adopted jqGrid as my new de facto grid control. From a end user’s perspective, jqGrid provides a top-notch user experience. Unfortunately, the control is heavily dependent on JavaScript so its not always the best solution for Mobile websites. In addition, jqGrid does require a fair amount of plumbing. Although it’s not difficult to implement it does take time and does not provide the rapid development experience that I grew accustomed to with the DataGridView control.

For about the last 12 months I went old school. Yes, I have manually been coding my tables by looping over a enumerable list and creating rows and columns. Just like I did ten years ago! Its ridiculous. Luckily, I recently stumbled upon the MVC Contrib Grid. In approximately 10 minutes, I downloaded the library, added a reference to the assembly and fully implemented a grid with paging and sorting! You will not believe how easy it is.

MVCContribGrid

Step 1: Creating the Controller Action

public ActionResult Manage()
{
    var categories = Repository.All<Category>();    
    return View(categories);
}


Step 2: Create the View Page

<%@ Import Namespace="MvcContrib.UI.Grid" %>
...
<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">    
    <%= Html.Grid(Model).Columns( column => {    
           column.For( x => x.Name );
           column.For( x => x.Description);
    })%>
</asp:Content>  

 

Step 3: Wait! I Need Sorting

A grid is no good unless it can sort. Time to modify the controller action and view to support sorting. First of all, you need to added a reference to MvcContib.Sorting and MvcContrib.UI.Grid. Next modify the method signature to accept a GridSortOptions object. This object contains the sorting information that was submitted from the view page.

 

using MvcContrib.Sorting;
using MvcContrib.UI.Grid;
...
public ActionResult Manage(GridSortOptions sort)
{
    var categories = Repository.All<Category>();
    if (sort.Column != null)            
        categories = categories.OrderBy(sort.Column, sort.Direction);            
    ViewData["sort"] = sort;
    return View(categories);
}
 

Ok, now we just need to modify the grid on the view page to show sortable column headers.

 

<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">    
    <%= Html.Grid(Model).Columns( column => {    
        column.For( x => x.Name ).Sortable(true);
        column.For( x => x.Description).Sortable(true);               
        }).Sort((GridSortOptions)ViewData["sort"])%>
    <%= Html.Pager((IPagination)Model) %>
</asp:Content>

 

Step 4: You Want Paging? No Problem.

Paging has never been easier. Just add another argument to your controller action which takes a nullable int named “page”.

 

using MvcContrib.UI.Grid;

...
        
public ActionResult Manage(GridSortOptions sort, int? page)
{
    var categories = Repository.All<Category>();
    if (sort.Column != null)            
        categories = categories.OrderBy(sort.Column, sort.Direction);
    ViewData["sort"] = sort;
    return View(categories.AsPagination(page ?? 1, 10));
}

 

Now we need to add the paging control on the view page:

 

<%@ Import Namespace="MvcContrib.UI.Grid" %>
<%@ Import Namespace="MvcContrib.UI.Paging" %>

...

<asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server">    
    <%= Html.Grid(Model).Columns( column => {    
        column.For( x => x.Name ).Sortable(true);
        column.For( x => x.Description).Sortable(true);
     }).Sort((GridSortOptions)ViewData["sort"])%>
     
    <%= Html.Pager((IPagination)Model) %>
</asp:Content>

Admittedly, the jqGrid does have a superior look and feel and provides paging and sorting without making a post back. Unfortunately, jqGrid does require a little bit of know-how and can be frustrating to implement. Not too mention it has a heavy dependency on JavaScript which can be problematic for mobile compliant websites. In contrast, the MVC Contrib Grid couldn’t be any simpler. So what are you waiting for? Try it yourself…

Tags: ,

ASPNet | dotNet | MVC

Comments

Roland Belgium, on 6/30/2010 5:26:12 AM Said:

Roland

There is a very good screencast done by Jeremy Skinner about the related topic:

http://www.viddler.com/explore/c4mvc/videos/38/

Check out his website:

http://www.jeremyskinner.co.uk/

topsy.com , on 6/30/2010 11:00:53 AM Said:

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Code Capers | Looking for an MVC Grid Control? Try MVC Contrib!
        [codecapers.com]
        on Topsy.com

Christopher Deutsch United States, on 7/1/2010 2:29:09 PM Said:

Christopher Deutsch

Good article.

I'm still waiting for an MVC grid that does paging and sorting via both AJAX and can gracefully degrade to using post backs.

It seems the more javascript people use in their sites, the less benefit their is to MVC. Still not sold on it, but not giving up either. Maybe if I wasn't intimately familiar with WebForms, Telerik Conrols, and the page lifecycle it would be easier for me to give them up. Instead MVC feels like an improved version of technology we had 10 years ago. Like you said, you're looping threw datasets manually creating HTML again. I thought I was done with that. Can't wait until I have to try re-create other Telerik Control functionality with MVC. Spending weeks on something that takes hours  with WebForms doesn't seem like progress, but at least your page is lighter weight (hopefully). ;)

Dave Swersky United States, on 7/1/2010 2:43:57 PM Said:

Dave Swersky

Nice to know this is out there, especially since jqGrid is now a paid control!

DotNetKicks.com , on 7/7/2010 12:48:13 AM Said:

trackback

Looking for an MVC Grid Control? Try MVC Contrib!

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetShoutout , on 7/7/2010 12:49:37 AM Said:

trackback

Code Capers | Looking for an MVC Grid Control? Try MVC Contrib!

Thank you for submitting this cool story - Trackback from DotNetShoutout

WebDevVote.com , on 7/7/2010 6:27:01 AM Said:

trackback

Looking for an MVC Grid Control? Try MVC Contrib!

You are voted (great) - Trackback from WebDevVote.com

Code Capers , on 7/15/2010 9:40:58 PM Said:

trackback

Building Better MVC Code with T4MVC

Building Better MVC Code with T4MVC

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading