0

I'm trying to understand how I can format some values in a simple table using the DisplayFormat attribute. @Html.DisplayFor seems to only support use cases where we're trying to directly format something on the model. But here the model is an enumeration of ViewModels.

The ViewModel:

class PersonViewModel
{
    public string Name { get; set; }

    [DisplayFormat(DataFormatString = @"{0:dd MMM yyyy}")]
    public DateTime? DateOfBirth { get; set; }

    [DisplayFormat(DataFormatString = @"{0:dd MMM yyyy}")]
    public DateTime? DateOfRegistration { get; set; }
}

And the view:

@model IEnumerable<PersonViewModel>

<table>
@foreach (var person in Model)
{
  <tr>
    <td>@person.Name</td>
    <td>@person.DateOfBirth</td>
    <td>@person.DateOfRegistration</td>
    <td>@Html.DisplayFor(model => model.???) <!-- can't use this here -->
  </tr>
}
</table>
Aetherix
  • 2,150
  • 3
  • 24
  • 44

0 Answers0