I have a simple ASP.NET MVC view based on a model. This model has the following property:
public DateTime PaymentStartDate { get; set; }
When I enter my view in the GET method, the value of PaymentStartDate
is 31th of March (31/3/2018 23:59:59).
In my view, I do the following:
@Html.HiddenFor(c => c.PaymentStartDate)
Which is rendered as follows:
<input data-val="true" data-val-date="The field PaymentStartDate must be a date." data-val-required="The PaymentStartDate field is required." id="PaymentStartDate" name="PaymentStartDate" type="hidden" value="03/31/2018 23:59:59" />
But when I POST to my endpoint, my modelstate is invalid because:
The value '03/31/2018 23:59:59' is not valid for PaymentStartDate.
THE PROBLEM:
If I pick a date, such as 3/12/2018
, it works. So my assumption is because I have a Danish machine (using dd/MM/yyyy format), it gives some problems.
Any ideas or hints what I can do here?
Always use ISO8601 notation for Date
and DateTime
values. Specifically when you store values as strings (like in the HTML in a hidden input field) and when sending values between 2 end points (like between browser and server).
That way there is no ambiguity regardless of client or server culture.
Example:
@Html.HiddenFor(c => c.PaymentStartDate.ToString("o"))