ビュー内のスパゲッティコードを減らすために、特定のHtmlHelperテーブル拡張を作成しようとしています。オブジェクトリストからMVC 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
}
だから私は思っていたようになったのはここですか?
クライアント側の柔軟性が必要な場合は、jqGridも検討してください。 – queen3