@Html.TextBoxFor(m => m.BeginDatum, "{0:dd-MM-yyyy}" /> @Html.TextBoxFor(m => m.BeginDatum, "{0:dd-MM-yyyy}" /> @Html.TextBoxFor(m => m.BeginDatum, "{0:dd-MM-yyyy}"/>

Html.TextBoxFor with Format does not work

210 Views Asked by At

For some reason the format of the code below is still displayed as dd/mm/yyyy.

    <div class="col-4">
        @Html.TextBoxFor(m => m.BeginDatum, "{0:dd-MM-yyyy}", new { id = "BeginDatum", @class = "form-control float-left key", placeholder = "dd-mm-jjjj" })
    </div>

In the viewmodel:

    [Mandatory]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
    public string BeginDatum { get; set; }

We are using MVC v5.2.3.

It makes no difference

  • in which browser the page is shown.
  • when the DisplayFormatAttribute is removed.
  • when the format is removed from the TextBoxFor.
1

There are 1 best solutions below

0
Paul Sinnema On
[Mandatory]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}")]
public DateTime BeginDatum { get; set; }

Found the problem. The type was string and should be DateTime.