【问题标题】:How to create MVC HtmlHelper table from list of objects如何从对象列表创建 MVC HtmlHelper 表
【发布时间】:2009-12-11 22:46:43
【问题描述】:

我正在尝试创建一个特定的 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
}

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

【问题讨论】:

    标签: c# asp.net-mvc properties html-helper


    【解决方案1】:

    到目前为止看起来不错,但 Gil Fink 可能已经在这里为您完成了工作:http://blogs.microsoft.co.il/blogs/gilf/archive/2009/01/13/extending-asp-net-mvc-htmlhelper-class.aspx

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 感谢网格的链接。我将对此进行研究,但目前我自己设法实现了它。
      【解决方案3】:

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

      基于 MVC 1.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);
          }
      }
      

      这让我可以用这个属性来装饰我的域对象...

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

      然后在 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);
      

      请注意,此解决方案在我的小应用程序中对我有用,但将更多地关注 MvcContrib 的 Grid 以获得更好的实施。

      【讨论】:

      • 如果您需要客户端灵活性,也可以考虑 jqGrid。
      猜你喜欢
      • 1970-01-01
      • 2017-06-26
      • 2021-03-02
      • 1970-01-01
      • 2011-09-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-12
      • 2014-07-22
      相关资源
      最近更新 更多