MvcContrib Grid Presentation

Today I did a presentation for the C4mvc virtual usergroup on the MvcContrib grid component

In the presentation I covered basic grid usage, auto-generated columns, paging, sorting and extensibility using custom grid renderers.

The sample code that I used in the presentation is available here. The video for the talk is available on viddler.

MvcContrib Grid Part 7 – Auto-generated columns

This is part 7 of a series of posts on the MvcContrib grid component.

The MvcContrib grid provides a way to automatically generate columns based on the public properties of an object. This is particularly useful when you are using a dedicated presentation model where the object properties map directly to the columns that you want to display.

To make use of this feature, you can call the AutoGenerateColumns method in the Grid declaration:

<%= Html.Grid(Model).AutoGenerateColumns() %>

By default, this will generate one column per public property and the name of the property will be used as the column heading (although PascalCased properties will have spaces inserted, eg “DateOfBirth” would be rendered as “Date Of Birth”).

This output can be customised by making use of MVC2’s ModelMetadata.

Out of the box, MVC2 uses the System.ComponentModel.DataAnnotations attributes to obtain metadata about a particular type (although you can also use other frameworks). The following attributes can be used to customise the auto-generated columns:

  • ScaffoldColumn – can be used to prevent a property from being rendered as a grid column
  • DisplayName – can be used to provide a custom column heading
  • DisplayFormat – can be used to specify a custom display format

Additional columns not on your presentation model can also be added by making a call to the normal Columns method after the call to AutoGenerateColumns. For example, imagine we have a CustomerPresentationModel:

public class CustomerPresentationModel {
 
   [ScaffoldColumn(false)]
   public int CustomerId { get; set; }
 
   public string Name { get; set; }
   public DateTime DateOfBirth { get; set; }
}

If a collection of these objects is passed to the Grid method, then two columns will be auto-generated – one for Name and one for DateOfBirth (CustomerId is excluded because it is decorated with ScaffoldColumn(false))

If we also want to add an “Edit” link for the customer, then we could add an additional column:

<%= Html.Grid(Model).AutoGenerateColumns().Columns(extraColumns => {
        extraColumns.For(x => Html.ActionLink("Edit Customer", "Edit", new{ id = x.CustomerId });
}) %>

The additional column contains a link to the edit page for each customer. Note that while columns are automatically HTML-encoded, the grid will detect that ActionLink returns an instance of MvcHtmlString and therefore won’t encode this particular column.

Additional columns can also be re-ordered. For example, if we wanted our “Edit” link to appear before the Name/DateOfBirth then the InsertAt method could be used:

<%= Html.Grid(Model).AutoGenerateColumns().Columns(extraColumns => {
        extraColumns.For(x => Html.ActionLink("Edit Customer", "Edit", new{ id = x.CustomerId })
            .InsertAt(0);
}) %>

Note that the InsertAt method is not contained in the latest MvcContrib release – you’ll need to build from source to get this.

I’ve also started to put together some documentation for the grid on the MvcContrib wiki.

Using Mercurial with Windows Powershell

Last month I wrote about using git with Windows Powershell by making use of a custom prompt function as well as tab expansion. My sample code for the git tab expansion and Mark’s custom prompt are now available on GitHub in the posh-git project.

I’ve also ported both the prompt and the tab expansion to work with Mercurial.

image

Much like the git prompt, the Mercurial prompt shows the name of the branch as well as other information. The prompt shows the following information:

  • “hg” – Identifies this as a mercurial repository (so I can tell whether I’m using git or mercurial)
  • “default” – We’re currently on the “default” branch
  • “+<number>” – Number of files that have been added but not yet committed
  • “~<number>” – Number of files that have been modified but not yet committed
  • “-<number>” – Number of files that have been removed but not yet committed
  • “?<number>” – Number of untracked files in the repository
  • “!<number>” – Number of missing files

I’ve also written some basic tab expansion functionality that provides completion for hg commands as well as hgtk commands (the TortoiseHg command line tool).

Code can be found in the posh-hg repository (somewhat ironically hosted on GitHub).

FluentValidation presentation – London – Wed 14 April

I’m going to be giving a short (20 minute) presentation about FluentValidation as part of the “OpenSource on .NET” event on Wednesday 14 April.

Other Open Source frameworks/libraries being covered are:

  • OpenRasta
  • Castle Windsor
  • CouchDB
  • IronRuby
  • Fluent NHibernate

If you’re interested in coming along, please register on Event Brite.

FluentValidation 1.2 RC available

The Release Candidate for FluentValidation 1.2 is now available.

This build contains minor bug fixes from 1.2 beta 3, the AssemblyScanner class that I mentioned in this post and compatibility with ASP.NET MVC 2 RTM.

If there are no serious bugs found with this build in the next couple of weeks then I will declare it as the “RTM” version.

As usual, binaries can be found on CodePlex and the source code is available on GitHub.