【发布时间】: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