我正在尝试提供一个轻量级模板,用于生成表格以表示模型对象列表并将指定字段显示为列。到目前为止,这是我提出的一些问题。
DataTable.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
namespace Models
{
public interface IDataTable
{
List<string> ColumnNames { get; }
List<List<string>> TableRows { get; }
}
public class DataTable<T> : IDataTable
{
private Type TableType = typeof(T);
public List<string> Columns { get; set; }
public List<T> RowData { get; set; }
public List<string> ColumnNames
{
get
{
List<string> columnNames = new List<string>();
foreach (string colPropName in Columns)
columnNames.Add(GetPropertyDisplayName(colPropName));
return columnNames;
}
}
public List<List<string>> TableRows
{
get
{
List<List<string>> tableRows = new List<List<string>>();
foreach (T rowObj in RowData)
{
List<string> tableRow = new List<string>();
foreach (string propName in Columns)
{
object value = TableType.GetProperty(propName).GetValue(rowObj, null);
if (value != null && value != String.Empty)
tableRow.Add(value.ToString());
else
tableRow.Add("N/A");
}
tableRows.Add(tableRow);
}
return tableRows;
}
}
public DataTable(List<string> columns, List<T> rowData)
{
Columns = columns;
RowData = rowData;
}
private string GetPropertyDisplayName(string propName)
{
DisplayNameAttribute attrib = TableType.GetProperty(propName).GetCustomAttributes(true).OfType<DisplayNameAttribute>().FirstOrDefault();
if (attrib != null)
return attrib.DisplayName;
return propName;
}
}
}
共享/ DataTable.cshtml:
@model IDataTable
<table class="rounded-corner-table" summary="2007 Major IT Companies' Profit">
<thead>
<tr>
@foreach (string colName in Model.ColumnNames)
{
<th scope="col">@colName</th>
}
</tr>
</thead>
<tfoot>
<tr>
<td colspan="@(Model.ColumnNames.Count - 1)" class="rounded-foot-left"><em>To be implemented: Instant search.</em></td>
<td class="rounded-foot-right"> </td>
</tr>
</tfoot>
<tbody>
@for (int i = 0; i < Model.TableRows.Count; ++i)
{
<tr>
@foreach (string colValue in Model.TableRows[i])
{
<td>@colValue</td>
}
</tr>
}
</tbody>
</table>
我如何使用它们:
public ActionResult List()
{
return PartialView(new DataTable<Administrator>(new List<string> { "EmailAddress",
"FirstName",
"LastName",
"Date" },
Root.DataContext.Administrators.ToList()));
}
//_______________________________________________________________________________
@model DataTable<Administrator>
@{Html.RenderPartial("DataTable");}
我遇到的两个主要问题是我希望能够调用Html.DisplayForModel()
但不知道如何使其工作,并且它没有显示列名的DisplayName属性表。有人可以就这些问题向我提出一些建议吗?
谢谢, 亚历克斯。
更新 - 修正了我的第二个问题:
private string GetPropertyDisplayName(string propName)
{
DisplayNameAttribute attrib = TableType.GetProperty(propName).GetCustomAttributes(true).OfType<DisplayNameAttribute>().FirstOrDefault();
if (attrib == null)
{
MetadataTypeAttribute metaAttrib = TableType.GetCustomAttributes(true).OfType<MetadataTypeAttribute>().FirstOrDefault();
if (metaAttrib != null)
attrib = metaAttrib.MetadataClassType.GetProperty(propName).GetCustomAttributes(true).OfType<DisplayNameAttribute>().FirstOrDefault();
}
if (attrib != null)
return attrib.DisplayName;
return propName;
}
答案 0 :(得分:0)
我估计你几乎就在那里。它看起来很不错。
根据Phil Haack的博客here设置表格显示模板怎么样?
然后您可以像Html.DisplayForModel("TableTemplateName")