This project is read-only.

System.ComponentModel.DataAnnotations for data annotations?

Jul 9, 2013 at 9:30 PM
Edited Jul 9, 2013 at 9:32 PM

I'm trying out Grid.Mvc and really liking it.

The current way to use data annotations is to use the attributes (such as GridColumnAttribute) that are part of the GridMvc.DataAnnotations namespace. I'd prefer to use the attributes in the System.ComponentModel.DataAnnotations namespace since I'm already using the DisplayAttribute for controlling field label names, plus I'm also using the MetadataTypeAttribute for creating a buddy class for annotating classes that are code generated in a database-first entity framework model.

Is the use of System.ComponentModel.DataAnnotations possible? If not, please consider using it for a future release for any functionality that overlaps with GridMvc.DataAnnotations.

Jul 10, 2013 at 5:11 AM
Now there is no way to use System.ComponentModel.DataAnnotations. I will include this feature in the next release. Thanks
Aug 12, 2013 at 4:03 PM
Thanks!! looking forward to it!!
Sep 13, 2013 at 4:40 PM
I'd second this request. It does seem that there is support for this when using AutoGenerateColumns, which uses GetAnnotationForColumn(). However, when declaring columns through expressions, it takes a different path that doesn't read the data annotations.

Another option which would be useful is allowing AutoGenerateColumns to navigate through subclasses. For example:

public Address Address { get; set; }

If I make progress implementing either of these, I'll send you an SVN patch.
Mar 19, 2015 at 11:18 PM
Could you please raise the priority on this? I'm converting all my MVC 5 tables that use @Html.DisplayNameFor and having to revert to hard coding column headers is a major step back for us.
Mar 23, 2015 at 2:02 PM
Never mind - Figured out how to use the System.ComponentModel.DataAnnotations.

You can use something like:
  columns.Add(r => r.Table.Column)
    .Titled(@Html.DisplayNameFor(r => r.Table.Column).ToString());
Jun 4, 2015 at 11:46 AM
Edited Jun 4, 2015 at 4:13 PM
I downloaded MVC.Grid source code and appended these codes into GridColumns.cs line 69:
(in constructor)

was :
Title = Name;
new :
                IEnumerable<PropertyInfo> ps = null;
                var p = PropertiesHelper.GetPropertyFromColumnName(Name, typeof(T), out ps);
                var attr = PropertiesHelper.GetAttribute<DisplayAttribute>(p);
                if (attr != null)
                    var prop = attr.ResourceType.GetProperty(attr.Name);
                    if (prop != null)
                        Title = prop.GetValue(p, null).ToString();
                        Title = Name;
                    Title = Name; //Using the same name by default