Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6068889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:45:48+00:00 2026-05-23T09:45:48+00:00

I am trying to come up with a light weight template for generating a

  • 0

I am trying to come up with a light weight template for generating a table to represent a list of model objects and display specified fields as columns. So far this is what I have come up with, with a few annoying issues.

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;
        }
    }
}

Shared/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">&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>

How I use Them:

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");}

The two main problems I am having with this is I would prefer to be able to call Html.DisplayForModel() but do not know how to make it work and that it is not displaying the DisplayName attributes for the column names in the table. Can someone please offer me some advice on these issues?

Thanks,
Alex.

UPDATE – Fixed my second issue:

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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:45:48+00:00Added an answer on May 23, 2026 at 9:45 am

    I reckon you are pretty much there. It looks pretty good.

    What about setting up a tabular display template, as per Phil Haack’s blog here?

    Then you can call it like this Html.DisplayForModel("TableTemplateName")

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We’re trying to come up with something approaching a simple and straight-forward model for
Trying to come up with a basic way to display code in a way
I am trying come up with a way to pull the tables out of
im trying to come up with a design for a wrapper for use when
I am trying to come up with the best way to render some hierarchical
I've been trying to come up with a way to create a 3 column
I'm trying to come up with a Java regex that will match a filename
I am trying to come up with such a solution that the user is
I am trying to come up with the best data structure for use in
We're trying to come up with a recommended design pattern for our team when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.