July 2011
Volume 26 Number 07
ASP.NET WebGrid - Get the Most out of WebGrid in ASP.NET MVC
By Stuart Leeks | July 2011
Earlier this year Microsoft released ASP.NET MVC version 3 (asp.net/mvc), as well as a new product called WebMatrix (asp.net/webmatrix). The WebMatrix release included a number of productivity helpers to simplify tasks such as rendering charts and tabular data. One of these helpers, WebGrid, lets you render tabular data in a very simple manner with support for custom formatting of columns, paging, sorting and asynchronous updates via AJAX.
In this article, I’ll introduce WebGrid and show how it can be used in ASP.NET MVC 3, then take a look at how to really get the most out of it in an ASP.NET MVC solution. (For an overview of WebMatrix—and the Razor syntax that will be used in this article—see Clark Sell’s article, “Introduction to WebMatrix,” in the April 2011 issue at msdn.microsoft.com/magazine/gg983489).
This article looks at how to fit the WebGrid component into an ASP.NET MVC environment to enable you to be productive when rendering tabular data. I’ll be focusing on WebGrid from an ASP.NET MVC aspect: creating a strongly typed version of WebGrid with full IntelliSense, hooking into the WebGrid support for server-side paging and adding AJAX functionality that degrades gracefully when scripting is disabled. The working samples build on top of a service that provides access to the AdventureWorksLT database via the Entity Framework. If you’re interested in the data-access code, it’s available in the code download, and you might also want to check out Julie Lerman’s article, “Server-Side Paging with the Entity Framework and ASP.NET MVC 3,” in the March 2011 issue (msdn.microsoft.com/magazine/gg650669).
Getting Started with WebGrid
To show a simple example of WebGrid, I’ve set up an ASP.NET MVC action that simply passes an IEnumerable<Product> to the view. I’m using the Razor view engine for most of this article, but later I’ll also discuss how the WebForms view engine can be used. My ProductController class has the following action:
public ActionResult List()
{
IEnumerable<Product> model =
_productService.GetProducts();
return View(model);
}
The List view includes the following Razor code, which renders the grid shown in Figure 1:
@model IEnumerable<MsdnMvcWebGrid.Domain.Product>
@{
ViewBag.Title = "Basic Web Grid";
}
<h2>Basic Web Grid</h2>
<div>
@{
var grid = new WebGrid(Model, defaultSort:"Name");
}
@grid.GetHtml()
</div>
Figure 1 A Basic Rendered Web Grid
The first line of the view specifies the model type (for example, the type of the Model property that we access in the view) to be IEnumerable<Product>. Inside the div element I then instantiate a WebGrid, passing in the model data; I do this inside an @{...} code block so that Razor knows not to try to render the result. In the constructor I also set the defaultSort parameter to “Name” so theWebGrid knows that the data passed to it is already sorted by Name. Finally, I use @grid.GetHtml() to generate the HTML for the grid and render it into the response.
This small amount of code provides rich grid functionality. The grid limits the amount of data displayed and includes pager links to move through the data; column headings are rendered as links to enable paging. You can specify a number of options in the WebGrid constructor and the GetHtml method in order to customize this behavior. The options let you disable paging and sorting, change the number of rows per page, change the text in the pager links and much more. Figure 2 shows the WebGrid constructor parameters and Figure 3 the GetHtml parameters.
Figure 2 WebGrid Constructor Parameters
Name | Type | Notes |
source | IEnumerable<dynamic> | The data to render. |
columnNames | IEnumerable<string> | Filters the columns that are rendered. |
defaultSort | string | Specifies the default column to sort by. |
rowsPerPage | int | Controls how many rows are rendered per page (default is 10). |
canPage | bool | Enables or disables paging of data. |
canSort | bool | Enables or disables sorting of data. |
ajaxUpdateContainerId | string | The ID of the grid’s containing element, which enables AJAX support. |
ajaxUpdateCallback | string | The client-side function to call when the AJAX update is complete. |
fieldNamePrefix | string | Prefix for query string fields to support multiple grids. |
pageFieldName | string | Query string field name for page number. |
selectionFieldName | string | Query string field name for selected row number. |
sortFieldName | string | Query string field name for sort column. |
sortDirectionFieldName | string | Query string field name for sort direction. |
Figure 3 WebGrid.GetHtml Parameters
Name | Type | Notes |
tableStyle | string | Table class for styling. |
headerStyle | string | Header row class for styling. |
footerStyle | string | Footer row class for styling. |
rowStyle | string | Row class for styling (odd rows only). |
alternatingRowStyle | string | Row class for styling (even rows only). |
selectedRowStyle | string | Selected row class for styling. |
caption | string | The string displayed as the table caption. |
displayHeader | bool | Indicates whether the header row should be displayed. |
fillEmptyRows | bool | Indicates whether the table can add empty rows to ensure the rowsPerPage row count. |
emptyRowCellValue | string | Value used to populate empty rows; only used when fillEmptyRows is set. |
columns | IEnumerable<WebGridColumn> | Column model for customizing column rendering. |
exclusions | IEnumerable<string> | Columns to exclude when auto-populating columns. |
mode | WebGridPagerModes | Modes for pager rendering (default is NextPrevious and Numeric). |
firstText | string | Text for a link to the first page. |
previousText | string | Text for a link to the previous page. |
nextText | string | Text for a link to the next page. |
lastText | string | Text for a link to the last page. |
numericLinksCount | int | Number of numeric links to display (default is 5). |
htmlAttributes | object | Contains the HTML attributes to set for the element. |
The previous Razor code will render all of the properties for each row, but you may want to limit which columns are displayed. There are a number of ways to achieve this. The first (and simplest) is to pass the set of columns to the WebGrid constructor. For example, this code renders just the Name and ListPrice properties:
var grid = new WebGrid(Model, columnNames: new[] {"Name", "ListPrice"});
You could also specify the columns in the call to GetHtml instead of in the constructor. While this is slightly longer, it has the advantage that you can specify additional information about how to render the columns. In the following example, I specified the header property to make the ListPrice column more user-friendly:
@grid.GetHtml(columns: grid.Columns(
grid.Column("Name"),
grid.Column("ListPrice", header:"List Price")
)
)
Often when you render a list of items, you want to let users click on an item to navigate to the Details view. The format parameter of the Column method allows you to customize the rendering of a data item. The following code shows how to change the rendering of names to output a link to the Details view for an item, and outputs the list price with two decimal places as typically expected for currency values; the resulting output is shown in Figure 4.
@grid.GetHtml(columns: grid.Columns(
grid.Column("Name", format: @<text>@Html.ActionLink((string)item.Name,
"Details", "Product", new {id=item.ProductId}, null)</text>),
grid.Column("ListPrice", header:"List Price",
format: @<text>@item.ListPrice.ToString("0.00")</text>)
)
)
Figure 4 A Basic Grid with Custom Columns
Although it looks like there’s some magic going on when I specify the format, the format parameter is actually a Func<dynamic,object>—a delegate that takes a dynamic parameter and returns an object. The Razor engine takes the snippet specified for the format parameter and turns it into a delegate. That delegate takes a dynamic parameter named item, and this is the item variable that’s used in the format snippet. For more information on the way these delegates work, see Phil Haack’s blog post at bit.ly/h0Q0Oz.
Because the item parameter is a dynamic type, you don’t get any IntelliSense or compiler checking when writing your code (see Alexandra Rusina’s article on dynamic types in the February 2011 issue at msdn.microsoft.com/magazine/gg598922). Moreover, invoking extension methods with dynamic parameters isn’t supported. This means that, when calling extension methods, you have to ensure that you’re using static types—this is the reason that item.Name is cast to a string when I call the Html.ActionLink extension method in the previous code. With the range of extension methods used in ASP.NET MVC, this clash between dynamic and extension methods can become tedious (even more so if you use something like T4MVC: bit.ly/9GMoup).
Adding Strong Typing
While dynamic typing is probably a good fit for WebMatrix, there are benefits to strongly typed views. One way to achieve this is to create a derived type WebGrid<T>, as shown in Figure 5. As you can see, it’s a pretty lightweight wrapper!
Figure 5 Creating a Derived WebGrid
public class WebGrid<T> : WebGrid
{
public WebGrid(
IEnumerable<T> source = null,
... parameter list omitted for brevity)
: base(
source.SafeCast<object>(),
... parameter list omitted for brevity)
{ }
public WebGridColumn Column(
string columnName = null,
string header = null,
Func<T, object> format = null,
string style = null,
bool canSort = true)
{
Func<dynamic, object> wrappedFormat = null;
if (format != null)
{
wrappedFormat = o => format((T)o.Value);
}
WebGridColumn column = base.Column(
columnName, header,
wrappedFormat, style, canSort);
return column;
}
public WebGrid<T> Bind(
IEnumerable<T> source,
IEnumerable<string> columnNames = null,
bool autoSortAndPage = true,
int rowCount = -1)
{
base.Bind(
source.SafeCast<object>(),
columnNames,
autoSortAndPage,
rowCount);
return this;
}
}
public static class WebGridExtensions
{
public static WebGrid<T> Grid<T>(
this HtmlHelper htmlHelper,
... parameter list omitted for brevity)
{
return new WebGrid<T>(
source,
... parameter list omitted for brevity);
}
}
So what does this give us? With the new WebGrid<T> implementation, I’ve added a new Column method that takes a Func<T, object> for the format parameter, which means that the cast isn’t required when calling extension methods. Also, you now get IntelliSense and compiler checking (assuming that MvcBuildViews is turned on in the project file; it’s turned off by default).
The Grid extension method allows you to take advantage of the compiler’s type inference for generic parameters. So, in this example, you can write Html.Grid(Model) rather than new WebGrid<Product>(Model). In each case, the returned type is WebGrid<Product>.
Adding Paging and Sorting
You’ve already seen that WebGrid gives you paging and sorting functionality without any effort on your part. You’ve also seen how to configure the page size via the rowsPerPage parameter (in the constructor or via the Html.Grid helper) so that the grid will automatically show a single page of data and render the paging controls to allow navigation between pages. However, the default behavior may not be quite what you want. To illustrate this, I’ve added code to render the number of items in the data source after the grid is rendered, as shown in Figure 6.
Figure 6 The Number of Items in the Data Source
As you can see, the data we’re passing contains the full list of products (295 of them in this example, but it’s not hard to imagine scenarios with even more data being retrieved). As the amount of data returned increases, you place more and more load on your services and databases, while still rendering the same single page of data. But there’s a better approach: server-side paging. In this case, you pull back only the data needed to display the current page (for instance, only five rows).
The first step in implementing server-side paging for WebGrid is to limit the data retrieved from the data source. To do this, you need to know which page is being requested so you can retrieve the correct page of data. When WebGrid renders the paging links, it reuses the page URL and attaches a query string parameter with the page number, such as https://localhost:27617/Product/DefaultPagingAndSorting?page=3 (the query string parameter name is configurable via the helper parameters—handy if you want to support pagination of more than one grid on a page). This means you can take a parameter called page on your action method and it will be populated with the query string value.
If you just modify the existing code to pass a single page worth of data to WebGrid, WebGrid will see only a single page of data. Because it has no knowledge that there are more pages, it will no longer render the pager controls. Fortunately, WebGrid has another method, named Bind, that you can use to specify the data. As well as accepting the data, Bind has a parameter that takes the total row count, allowing it to calculate the number of pages. In order to use this method, the List action needs to be updated to retrieve the extra information to pass to the view, as shown in Figure 7.
Figure 7 Updating the List Action
public ActionResult List(int page = 1)
{
const int pageSize = 5;
int totalRecords;
IEnumerable<Product> products = productService.GetProducts(
out totalRecords, pageSize:pageSize, pageIndex:page-1);
PagedProductsModel model = new PagedProductsModel
{
PageSize= pageSize,
PageNumber = page,
Products = products,
TotalRows = totalRecords
};
return View(model);
}
With this additional information, the view can be updated to use the WebGrid Bind method. The call to Bind provides the data to render and the total number of rows, and also sets the autoSortAndPage parameter to false. The autoSortAndPage parameter instructs WebGrid that it doesn’t need to apply paging, because the List action method is taking care of this. This is illustrated in the following code:
<div>
@{
var grid = new WebGrid<Product>(null, rowsPerPage: Model.PageSize,
defaultSort:"Name");
grid.Bind(Model.Products, rowCount: Model.TotalRows, autoSortAndPage: false);
}
@grid.GetHtml(columns: grid.Columns(
grid.Column("Name", format: @<text>@Html.ActionLink(item.Name,
"Details", "Product", new { id = item.ProductId }, null)</text>),
grid.Column("ListPrice", header: "List Price",
format: @<text>@item.ListPrice.ToString("0.00")</text>)
)
)
</div>
With these changes in place, WebGrid springs back to life, rendering the paging controls but with the paging happening in the service rather than in the view! However, with autoSortAndPage turned off, the sorting functionality is broken. WebGrid uses query string parameters to pass the sort column and direction, but we instructed it not to perform the sorting. The fix is to add the sort and sortDir parameters to the action method and pass these through to the service so that it can perform the necessary sorting, as shown in Figure 8.
Figure 8 Adding Sorting Parameters to the Action Method
public ActionResult List(
int page = 1,
string sort = "Name",
string sortDir = "Ascending" )
{
const int pageSize = 5;
int totalRecords;
IEnumerable<Product> products =
_productService.GetProducts(out totalRecords,
pageSize: pageSize,
pageIndex: page - 1,
sort:sort,
sortOrder:GetSortDirection(sortDir)
);
PagedProductsModel model = new PagedProductsModel
{
PageSize = pageSize,
PageNumber = page,
Products = products,
TotalRows = totalRecords
};
return View(model);
}
AJAX: Client-Side Changes
WebGrid supports asynchronously updating the grid content using AJAX. To take advantage of this, you just have to ensure the div that contains the grid has an id, and then pass this id in the ajaxUpdateContainerId parameter to the grid’s constructor. You also need a reference to jQuery, but that’s already included in the layout view. When the ajaxUpdateContainerId is specified, WebGrid modifies its behavior so that the links for paging and sorting use AJAX for the updates:
<div id="grid">
@{
var grid = new WebGrid<Product>(null, rowsPerPage: Model.PageSize,
defaultSort: "Name", ajaxUpdateContainerId: "grid");
grid.Bind(Model.Products, autoSortAndPage: false, rowCount: Model.TotalRows);
}
@grid.GetHtml(columns: grid.Columns(
grid.Column("Name", format: @<text>@Html.ActionLink(item.Name,
"Details", "Product", new { id = item.ProductId }, null)</text>),
grid.Column("ListPrice", header: "List Price",
format: @<text>@item.ListPrice.ToString("0.00")</text>)
)
)
</div>
While the built-in functionality for using AJAX is good, the generated output doesn’t function if scripting is disabled. The reason for this is that, in AJAX mode, WebGrid renders anchor tags with the href set to “#,” and injects the AJAX behavior via the onclick handler.
I’m always keen to create pages that degrade gracefully when scripting is disabled, and generally find that the best way to achieve this is through progressive enhancement (basically having a page that functions without scripting that’s enriched with the addition of scripting). To achieve this, you can revert back to the non-AJAX WebGrid and create the script in Figure 9 to reapply the AJAX behavior:
Figure 9 Reapplying the AJAX Behavior
$(document).ready(function () {
function updateGrid(e) {
e.preventDefault();
var url = $(this).attr('href');
var grid = $(this).parents('.ajaxGrid');
var id = grid.attr('id');
grid.load(url + ' #' + id);
};
$('.ajaxGrid table thead tr a').live('click', updateGrid);
$('.ajaxGrid table tfoot tr a').live('click', updateGrid);
});
To allow the script to be applied just to a WebGrid, it uses jQuery selectors to identify elements with the ajaxGrid class set. The script establishes click handlers for the sorting and paging links (identified via the table header or footer inside the grid container) using the jQuery live method (api.jquery.com/live). This sets up the event handler for existing and future elements that match the selector, which is handy given the script will be replacing the content.
The updateGrid method is set as the event handler and the first thing it does is to call preventDefault to suppress the default behavior. After that it gets the URL to use (from the href attribute on the anchor tag) and then makes an AJAX call to load the updated content into the container element. To use this approach, ensure that the default WebGrid AJAX behavior is disabled, add the ajaxGrid class to the container div and then include the script from Figure 9.
AJAX: Server-Side Changes
One additional point to call out is that the script uses functionality in the jQuery load method to isolate a fragment from the returned document. Simply calling load(‘https://example.com/someurl’) will load the contents of the URL. However, load(‘https://example.com/someurl #someId’) will load the content from the specified URL and then return the fragment with the id of “someId.” This mirrors the default AJAX behavior of WebGrid and means that you don’t have to update your server code to add partial rendering behavior; WebGrid will load the full page and then strip out the new grid from it.
In terms of quickly getting AJAX functionality this is great, but it means you’re sending more data over the wire than is necessary, and potentially looking up more data on the server than you need to as well. Fortunately, ASP.NET MVC makes dealing with this pretty simple. The basic idea is to extract the rendering that you want to share in the AJAX and non-AJAX requests into a partial view. The List action in the controller can then either render just the partial view for AJAX calls or the full view (which in turn uses the partial view) for the non-AJAX calls.
The approach can be as simple as testing the result of the Request.IsAjaxRequest extension method from inside your action method. This can work well if there are only very minor differences between the AJAX and non-AJAX code paths. However, often there are more significant differences (for example, the full rendering requires more data than the partial rendering). In this scenario you’d probably write an AjaxAttribute so you could write separate methods and then have the MVC framework pick the right method based on whether the request is an AJAX request (in the same way that the HttpGet and HttpPost attributes work). For an example of this, see my blog post at bit.ly/eMlIxU.
WebGrid and the WebForms View Engine
So far, all of the examples outlined have used the Razor view engine. In the simplest case, you don’t need to change anything to use WebGrid with the WebForms view engine (aside from differences in view engine syntax). In the preceding examples, I showed how you can customize the rendering of row data using the format parameter:
grid.Column("Name",
format: @<text>@Html.ActionLink((string)item.Name,
"Details", "Product", new { id = item.ProductId }, null)</text>),
The format parameter is actually a Func, but the Razor view engine hides that from us. But you’re free to pass a Func—for example, you could use a lambda expression:
grid.Column("Name",
format: item => Html.ActionLink((string)item.Name,
"Details", "Product", new { id = item.ProductId }, null)),
Armed with this simple transition, you can now easily take advantage of WebGrid with the WebForms view engine!
Wrapping Up
In this article I showed how a few simple tweaks let you take advantage of the functionality that WebGrid brings without sacrificing strong typing, IntelliSense or efficient server-side paging. WebGrid has some great functionality to help make you productive when you need to render tabular data. I hope this article gave you a feel for how to make the most of it in an ASP.NET MVC application.
Stuart Leeks is an application development manager for the Premier Support for Development team in the United Kingdom. He has an unhealthy love of keyboard shortcuts. He maintains a blog at blogs.msdn.com/b/stuartleeks where he talks about technical topics that interest him (including, but not limited to, ASP.NET MVC, Entity Framework and LINQ).
Thanks to the following technical experts for reviewing this article: Simon Ince and Carl Nolan