How do I get the [Display(Name="Some Title")]
DataAnnotations "Some Title" rendered in the List scaffold view's output?
I create a strongly typed list scaffold view for this class:
public class CompanyHoliday
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name = "Datum")]
[DataType(System.ComponentModel.DataAnnotations.DataType.Date)]
public DateTime Date { get; set; }
[Required]
[StringLength(50)]
[Display(Name = "Feiertag Name")]
public string Name { get; set; }
}
The view looks like this:
@model IEnumerable<Zeiterfassung.Domain.CompanyHoliday>
@{
ViewBag.Title = "Year";
}
<h2>Year</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table>
<tr>
<th></th>
<th>
Date
</th>
<th>
Name
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
@Html.ActionLink("Details", "Details", new { id=item.Id }) |
@Html.ActionLink("Delete", "Delete", new { id=item.Id })
</td>
<td>
@String.Format("{0:g}", item.Date)
</td>
<td>
@item.Name
</td>
</tr>
}
</table>
However, I don't want "Date" and "Name" in the table header. I want "Datum" and "Feiertag Name" rendered there dynamically.
The actual column header titles should come from the Display DataAnnotation.
How do I do this?
Bit of an old topic, but I came up with an extension method to handle this.
Lets say my model is:
Drop this method into a static class:
Then in your View, which is taking an
IEnumerable<MyModel>
, you can use it like this:The generated HTML will be:
Hope it helps!
This is the pattern I've followed. This assumes Lists are never null, but can be empty, but condition can easily be short circuited for a null list. I do like Brian's extension method though.