How to: RAD ASP.NET MVC with MvcContrib

ASP.NET MVC has many obvious merits – testability, separation of concerns, RESTful URLs etc. But one thing it notoriously doesn’t have is much support for Rapid Application Development. You have complete control over HTML… at the cost of writing all the HTML yourself. With ASP.NET Web Forms you might struggle to take complete control over the HTML, but it provides an enormous amount of support for RAD via controls that write the HTML for you.

Wouldn’t it be nice if we could have something similar in ASP.NET MVC? That’s where MvcContrib comes in.

In an earlier post, I introduced you to MvcContrib’s TestHelper. Another useful feature it offers is an HTML helper that generates grids – removing all the tedious donkey-work of writing the HTML table yourself, and taking a big step in the RAD direction.

The first thing you need to do is download MvcContrib from codeplex and set a reference to it inside your project. That gives you access to the MvcContrib.Ui.Grid namespace, and the Html.Grid() method. Now you can create a grid without having to write all the HTML yourself.

First, create a controller method that passes a strongly typed IEnumerable to the View. Here is a controller method that retrieves a list of Game objects and passes them to the view:

method returning IEnumerable to a view

Your view should be strongly typed to the Model, and have a using/Imports statement for MvcContrib.UI.Grid:

strongly typed view with using statement

Then all you have to do is pass the Model to Html.Grid(), and use a Lambda expression to define the columns:

call to the Grid() method

This lays out a Grid. In this case it’s a very simple grid (essentially just a table), but with a little work you can add support for the more advanced Grid features you would expect from RAD controls, such as pagination, sortability and custom columns.

the resulting table

So there you have it – a form of RAD that’s available for ASP.NET MVC. It’s not drag-and-drop, but it is rapid, and a lot more productive than writing all your own HTML.

Kevin Rattan

For related information, check out these courses from Learning Tree:

Building Web Applications with ASP.NET MVC

Building Web Applications with ASP.NET and Ajax

Type to search

Do you mean "" ?

Sorry, no results were found for your query.

Please check your spelling and try your search again.