views:

572

answers:

3

I am trying to create a specific HtmlHelper table extension to reduce the spaghetti code in my View.

Taking a list of domain objects I would like to display a table that is a little bit more intelligent in using the properties of the domain object as columns. In addition, I would like to disable showing of some properties as columns. An idea would be to decorate properties with attributes that tell it not to be shown.

Hopefully that makes sense but here's where I got to so far...

public static string MyTable(this HtmlHelper helper, string name, 
    IList<MyObject> items, object tableAttributes)
{
    if (items == null || items.Count == 0)
        return String.Empty;

    StringBuilder sb = new StringBuilder();
    BuildTableHeader(sb, items[0].GetType());

    //TODO: to be implemented...
    //foreach (var i in items)
    //    BuildMyObjectTableRow(sb, i);

    TagBuilder builder = new TagBuilder("table");
    builder.MergeAttributes(new RouteValueDictionary(tableAttributes));
    builder.MergeAttribute("name", name);
    builder.InnerHtml = sb.ToString();

    return builder.ToString(TagRenderMode.Normal);
}

private static void BuildTableHeader(StringBuilder sb, Type p)
{
    sb.AppendLine("<tr>");

    //some how here determine if this property should be shown or not
    //this could possibly come from an attribute defined on the property        
    foreach (var property in p.GetProperties())
        sb.AppendFormat("<th>{0}</th>", property.Name);

    sb.AppendLine("</tr>");
}

//would be nice to do something like this below to determine what
//should be shown in the table
[TableBind(Include="Property1,Property2,Property3")]
public partial class MyObject
{
   ...properties are defined as Linq2Sql
}

So I was just wondering if anyone had any opinions/suggestions on this idea or any alternatives?

A: 

Looks good so far, but Gil Fink may have already done the work for you here: http://blogs.microsoft.co.il/blogs/gilf/archive/2009/01/13/extending-asp-net-mvc-htmlhelper-class.aspx

Robert Harvey
A: 

I strongly suggest to use MvcContrib's Grid. If you decide not to, at least you can take a look at how they solved the table generation interface problem.

queen3
Thanks for the link to the Grid. I will be taking a look at this, however for the time being I managed to implement it myself.
David Liddle
A: 

After about an hour of work I was able to create what I wanted. My solution was to create an attribtue on the domain object class which specified which properties were visible in my table.

Based on the BindAttribute attribute in MVC 1.0 (having a look at the source code), I created a TableProperty attribute.

[AttributeUsage(AttributeTargets.Class, AllowMultiple = false, Inherited = true)]
public class TableProperty : Attribute
{
    private string m_include;
    private string[] m_includeSplit;

    public TableProperty()
    {
        m_includeSplit = new string[0];
    }

    public string Include
    {
        get
        {
            return (m_include ?? string.Empty);
        }
        set
        {
            m_include = value;
            m_includeSplit = value.Split(',');
        }
    }

    public bool IsPropertyAllowed(string propertyName)
    {
        return IsPropertyAllowed(propertyName, m_includeSplit);
    }

    internal static bool IsPropertyAllowed(string propertyName, string[] includeProperties)
    {
        return ((includeProperties == null) || (includeProperties.Length == 0)) || includeProperties.Contains<string>(propertyName, StringComparer.OrdinalIgnoreCase);
    }
}

This allowed me to decorate my domain object with this attribute...

[TableProperty(Include="Property1,Property2,Property3")]
public partial class MyObject
{ ...

Then inside the BuildTableHeader I used reflection to get the properties of the object and match each property to the allowed list.

private static void BuildTableHeader(StringBuilder sb, Type p)
{
    sb.AppendLine("<tr>");

    TableProperty tp = p.GetCustomAttributes(typeof(TableProperty), true)[0];

    foreach (var property in p.GetProperties())
        if (tp.IsPropertyAllowed(property.Name))
            sb.AppendFormat("<th>{0}</th>", property.Name);

Please note this solution worked for me in my little application however will be looking more at MvcContrib's Grid for a better implementation.

David Liddle
Also consider jqGrid if you need client-side flexibility.
queen3