I checked 10 thousand posts already, none of them did the trick about this very simple problem.
On my UI I have a TextBoxFor
for a DateTime property (on my model) very standard stuff.
Model
[DataType(DataType.Date)]
public DateTime DateTo { get; set; }
View
<div class="Label">
@Html.LabelFor(model => model.DateTo)
</div>
<div class="Control">
@Html.TextBoxFor(model => model.DateTo, "{0:dd/MM/yyyy}", new { @class = "datepicker" })
@Html.ValidationMessageFor(model => model.DateTo)
</div>
jQuery / DatePicker
Just in case, this is the code behind datepicker, I think it's pretty standard from jQuery datepicker, note that In the UI I see datepicker working as expected .
$(".datepicker").datepicker({ dateFormat: "dd/mm/yy", changeYear: true, showAnim: 'blind' });
Culture
So what I learned from other posts, you could change culture to achieve this, but didn't work, I overrided OnActionExecuting
with this code:
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("ES-AR");
Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("ES-AR");
I can see in the Inspection window, that the culture is "es-ar", but problem persists.
Problems!
Problem 1: If I wrote something like '01/02/2018', MVC automatic mapping thinks it's 2nd of January... But actually is meant to be 1st of February.
Problem 2: If I wrote something like '31/03/2018', MVC automatic mapping thinks it's a wrong date (like month 30) but actually is meant to be 30th of march.
Even page validator says " The value '31/03/2018' is not valid for DateTo.
"
What am I missing here?
oookey, while explaining this, I got the solution (rubber ducked stackoverflow :/ )...
Solution, It works fine if you put this in your config:
<globalization culture="es-AR" uiCulture="es-AR" />
The new problem I see , what if I wan't to change culture realtime to go back to EN-US? (To change thread culture seems to be the solution, but didn't work as I mentioned above)
For now, it solved my problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.