1

I m getting this borring error when I try formatting the date using ToString("dd-MM-YYYY") in asp.net mvc 5. Is the a way to do that ? And I dont understand why this error happen

Thanks in advance

My class

public class Registro : Numero
{
    public DateTime Date { get; set; }
    public decimal Valor { get; set; }
    public EnumConducao Conducao { get; set; }
    public EnumDestino Destino { get; set; }

}

My view

<table class="table">
        <tr>
            <th>
                @Html.DisplayNameFor(model => model.Date)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Valor)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Conducao)
            </th>
            <th>
                @Html.DisplayNameFor(model => model.Destino)
            </th>
            <th></th>
        </tr>

        @foreach (var item in Model) {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Date.ToString("dd-MM-YYYY"))
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Valor)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Conducao)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Destino)
                </td>
                <td>
                    @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
                    @Html.ActionLink("Delete", "Delete", new { id=item.Id })
                </td>
            </tr>
        }

</table>
  • Its just `item.Date.ToString("dd-MM-yyyy")` or if you want to make use of `DisplayFor()`, then add a `[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]` attribute to your property and then its just `@Html.DisplayFor(modelItem => item.Date)` –  Aug 28 '17 at 07:18

0 Answers0