GridColumnModel enhancements

Jun 7, 2011 at 8:12 PM

It looks like GridColumnModel has two ways specifies grid columns:

First way is to specify,

GridColumnModel("ProductName")

Two problems with this approach. One, it uses column name as a string, so any typo won't be recognized until runtime. And many other reasons why magic strings are evil. Two, you have to specify the column name manually - it doesn't leverage DataAnnotations values that model has.

Second way is to specify

GridColumnModel<Product>(c => c.ProductName)
Much better! The only grudge here is that Product is usually the Model that is passed into the view. So, I would prefer to have something like
GridColumnModelFor(c => c.ProductName)
I tried to tinker with GridColumnModel.cs - but my coding skills are not nearly that advanced :)

Coordinator
Jun 8, 2011 at 5:54 PM
Edited Jun 8, 2011 at 5:55 PM

Hi,

Since views are not always bound to a strongly typed model (e.g anonymous types, dynamic classes), I figured it would be good to have the option to bind a column to a string (as you correctly show in your first way).

However, when using anyone of this methods, you can still specify a caption (and any other option) for the column explicitly to allow better flexability, for instance:

GridColumnModel("ProductName").SetCaption("Product Name"), Or:

GridColumnModel<Product>(c=>c.ProductName).SetCaption("Product Name")

The reason why the generic variable is under GridColumnModel and not over the entire Grid (which would enable the use of GridColumnModelFor(c=>c.ProductName) as you mentioned) is mainly for backward compatability, but it seemed to - if it makes any sense - wrong to bind the hole grid to the model used only by it's columns.