首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >轻松显示表格数据?

轻松显示表格数据?
EN

Stack Overflow用户
提问于 2011-06-26 15:35:13
回答 1查看 619关注 0票数 0

我正在尝试想出一个轻量级的模板,用于生成一个表格来表示模型对象的列表,并将指定的字段显示为列。到目前为止,这就是我提出的一些令人恼火的问题。

DataTable.cs:

代码语言:javascript
复制
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:

代码语言:javascript
复制
@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">&nbsp;</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>

我如何使用它们:

代码语言:javascript
复制
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属性。有人能在这些问题上给我一些建议吗?

谢谢,亚历克斯。

更新-修复了我的第二个问题:

代码语言:javascript
复制
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;
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-06-26 21:57:48

我估计你差不多就在那儿了。看起来很不错。

根据Phil Haack的博客here,如何设置一个表格显示模板

然后您可以像这样将其命名为Html.DisplayForModel("TableTemplateName")

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6482724

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档