如何从对象列表创建MVC的HtmlHelper表 [英] How to create MVC HtmlHelper table from list of objects

查看:138
本文介绍了如何从对象列表创建MVC的HtmlHelper表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个特定的HtmlHelper表扩展来降低意大利面条code在我的视野。

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?

推荐答案

大约一​​个小时的工作后,我能创造我想要的东西。我的解决方案是建立在其指定的属性在我的表中可见的域对象类attribtue。

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.

(有一个看看源$ C ​​$ c)中,我创建了一个TableProperty属性。

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
{ ...

然后BuildTableHeader里面我用反射来的每个属性获取对象的属性,并匹配到允许列表中。

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);

请注意,这个解决方案在我的小应用程序为我工作,但将在MvcContrib的网格为更好地实施寻找更多。

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

这篇关于如何从对象列表创建MVC的HtmlHelper表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆