0
我想要一個輕量級的模板來生成一個表來表示模型對象列表並將指定的字段顯示爲列。到目前爲止,這是我提出的,有一些令人討厭的問題。容易顯示錶格化數據嗎?
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列表中的列名稱的DisplayName屬性。有人可以就這些問題向我提供一些建議嗎?
謝謝, Alex。
更新 - 修正了我的第二個問題:
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;
}
我覺得努力推廣這樣的東西是一個無底洞 - 只寫每個應用表的代碼 - 它會爲您節省時間和農業。 – iwayneo