简体   繁体   中英

Get the DisplayName of a property on an arbitrary type

I'm having trouble figuring out a clean way to do this. I have a ViewModel class that contains a collection of rows for a table. I would like to be able to do an @Html.DisplayNameFor() on the type in the strongly-typed collection without referring to the first item in the collection or creating a new instance of the type. So to clarify here's an example:

public class TableViewModel
{
  public string Title { get; set; }
  public IEnumerable<Row> Rows { get; set; }
}

public class Row
{
  public int ColumnA { get; set; }
  public int ColumnB { get; set; }
}

//In the Razor view
<table>
  <tr>
    <th>@Html.DisplayNameFor(???)</th>
  </tr>
</table>

Is there a way to do this without doing @Html.DisplayNameFor(x => x.Rows.First().ColumnA) ?

You could use the this.ViewData.ModelMetadata.Properties collection and call the GetDisplayName method to generate the header cell content. We do this generically in our code to render an arbitrary model as a table.

Edit: To expand on this, I use a basic model that represents a table and only a table. I have 3 classes defined. I have a TableRowCell' class, a TableRow' class (which is essentially a collection of TableRowCell objects) and a Table class.

public class Table
{
    public Table(String name)
    {
        this.Name = name;
        this.ContentRows = new List<TableRow>();
        this.HeaderRow = new TableRow();
        this.FooterRow = new TableRow();
    } 

    public IList<TableRow> ContentRows
    {
        get;
        private set;
    }

    public TableRow FooterRow
    {
        get;
        private set;
    }

    public TableRow HeaderRow
    {
        get;
        private set;
    }

    public String Name
    {
        get;
        private set;
    }
}

When I have a view model that contains a collection of objects that I want to display in a table I call first an HtmlHelper extension that converts that collection into a Table object. Inside that method I iterate over a call to ModelMetadataProviders.Current.GetMetadataForType(null, typeof(TModel)).Properties..Where(item => item.ShowForDisplay) to get a collection of metadata objects used to generate the header cells. I then iterate over the collection of items and call ModelMetadataProviders.Current.GetMetadataForType(() => item, typeof(TModel)).Properties..Where(item => item.ShowForDisplay) to get a collection of metadata objects used to generate the content cells.

Technically, my HtmlHelper extension method actually returns a TableBuilder object which will has a method called Render on it which generates the html. That setup has served me well so far.

public class TableViewModel
{
  public string Title { get; set; }
  public IEnumerable<Row> Rows { get; set; }
  public Row ForNames = null;
}


@Html.DisplayNameFor(x => x.ForName.ColumnA)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM