C# 如何从对象列表创建MVC HtmlHelper表

C# 如何从对象列表创建MVC HtmlHelper表,c#,asp.net-mvc,properties,html-helper,C#,Asp.net Mvc,Properties,Html Helper,我试图创建一个特定的HtmlHelper表扩展,以减少视图中的意大利面代码 以域对象列表为例,我想显示一个表,该表在将域对象的属性用作列时稍微智能一些。此外,我想禁用将某些属性显示为列。一个想法是用告诉它不要显示的属性来装饰房产 希望这是有道理的,但这就是我到目前为止所做的 public static string MyTable(this HtmlHelper helper, string name, IList<MyObject> items, object table

我试图创建一个特定的HtmlHelper表扩展,以减少视图中的意大利面代码

以域对象列表为例,我想显示一个表,该表在将域对象的属性用作列时稍微智能一些。此外,我想禁用将某些属性显示为列。一个想法是用告诉它不要显示的属性来装饰房产

希望这是有道理的,但这就是我到目前为止所做的

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
}
公共静态字符串MyTable(此HtmlHelper帮助程序,字符串名称,
IList项、对象表属性)
{
if(items==null | | items.Count==0)
返回字符串。空;
StringBuilder sb=新的StringBuilder();
BuildTableHeader(sb,项[0].GetType());
//TODO:即将实施。。。
//foreach(项目中的var i)
//BuildMyObjectTableRow(sb,i);
标记生成器=新标记生成器(“表”);
合并属性(新的RouteValueDictionary(tableAttributes));
builder.MergeAttribute(“名称”,名称);
builder.InnerHtml=sb.ToString();
返回builder.ToString(TagRenderMode.Normal);
}
私有静态void BuildTableHeader(StringBuilder sb,类型p)
{
某人加上一行(“”);
//这里介绍了如何确定是否应显示此属性
//这可能来自属性上定义的属性
foreach(p.GetProperties()中的var属性)
sb.AppendFormat(“{0}”,property.Name);
某人加上一行(“”);
}
//最好做下面这样的事情来确定
//应在表中显示
[TableBind(Include=“Property1,Property2,Property3”)]
公共部分类MyObject
{
…属性定义为Linq2Sql
}

所以我只是想知道是否有人对这个想法或任何替代方案有任何意见/建议?

到目前为止看起来不错,但Gil Fink可能已经在这里为您完成了工作:

我强烈建议使用MvcContrib的。如果您决定不这样做,至少您可以看看他们是如何解决表生成接口问题的。

经过大约一个小时的工作,我能够创建我想要的。我的解决方案是在域对象类上创建一个attribtue,它指定哪些属性在我的表中可见

基于MVC1.0中的BindAttribute属性(查看源代码),我创建了一个TableProperty属性

[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);
    }
}
然后在BuildTableHeader中,我使用反射来获取对象的属性,并将每个属性与允许的列表相匹配

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);
私有静态void BuildTableHeader(StringBuilder sb,类型p)
{
某人加上一行(“”);
TableProperty tp=p.GetCustomAttributes(typeof(TableProperty),true)[0];
foreach(p.GetProperties()中的var属性)
if(tp.IsPropertyAllowed(property.Name))
sb.AppendFormat(“{0}”,property.Name);

请注意,在我的小应用程序中,这个解决方案对我来说是有效的。但是,我们将更多地关注MVCCONTIB的网格,以便更好地实现。

感谢链接到网格。我将对此进行研究,但是暂时我自己实现了。如果需要客户端灵活性,也可以考虑JQGRID。
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);