I have tried several methods, but I don't know why, but this is confusing me.
Model:
public class SearchViewModel
{
[DisplayName("Date from:")]
//[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{dd.MM.yyyy}")]
public DateTime? DateFrom { get; set; }
[DisplayName("Date to:")]
public DateTime? DateTo { get; set; }
}
View:
@using (Html.BeginForm("SearchResult", "Search", FormMethod.Get, new { @id = "searchForDate" }))
{
<div id="criterias">
@Html.LabelFor(m => m.DateFrom)
@Html.EditorFor(m => m.DateFrom, new { htmlAttributes = new { @class = "searchinput datepicker" } })
@Html.LabelFor(m => m.DateTo)
@Html.EditorFor(m => m.DateTo, new { htmlAttributes = new { @class = "searchinput datepicker" } })
</div>
<button type="submit" class="btn btn-danger" style="background-color:#991821; margin-top:15px;" id="search">Search</button>
}
The problem:
The datepicker works fine. When I am loading the page, I click into the EditorFor DateFrom
box and picking a date, lets say 02.01.2019 (2nd of January 2019). When I click on submit it is jumping into the controller action.
In the controller action model.DateFrom
is changing day and month. That means it suddenly is 01.02.2019 (2nd of February), which is totally wrong. When I pick a day above 12
the model.DateFrom
is invalid.
I tried the following things:
- Insert
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{dd.MM.yyyy}")]
in the model - added
<system.web> <globalization uiCulture="de-DE" culture="de-DE" /></system.web>
in myWeb.config
- added the InternationalizationAttribute -->
CurrentCulture
andCurrentUICulture
in the Controller - tried to modify the
EditorFor
toTextboxFor
with several settings: see Date only from TextBoxFor()
I also came across this article: MVC DateTime binding with incorrect date format but unfortunately my know how isn't that good.
I don't know why this is happening and more importent WHERE this is happening? I put my breakpoint into the Internationalization class, put the bp direct under the SearchController
action. nothing helps. When I click the submit button, day and month are chaning the position...