0

I am trying to load in a DateTime value into ASP.NET's built-in date picker. I am successfully retrieving the value date from another source, but the date is not properly displaying. Instead of displaying the value, for example, as 12/02/2015, it incorrectly displays as mm/dd/yyyy (the default value before any date is entered). I don't think the problem is in the controller because the value is retrieved fine.

TO CLARIFY: the problem is that the DateTime does not get displayed at all in the view. It is literally "mm/dd/yyyy", and not a date. The problem isn't that I need the date in a different orientation like dd/mm/yyyy or yyyy/dd/mm.

This section of the model looks like:

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? myDate { get; set; }

This section of the controller looks like:

var a = dateFromDataBase; // string
model.myDate = string.IsNullOrEmpty(a.AnswerText) ? (DateTime?) null : Convert.ToDateTime(a.AnswerText); // model.myDate gets vaue, but displays as mm/dd/yyyy, converted to a DateTime value

This section of the view looks like:

@Html.ValidationMessageFor(model => model.myDate)
@Html.EditorFor(model => model.myDate)
tonyleMill
  • 87
  • 1
  • 12

1 Answers1

0

Could you paste more of the actual controller code?

For example, where does a.AnswerText come from?

Have you set breakpoints to make sure your model.myDate property gets populated with an actual datetime value?

If variable 'a' contains a string with a date format:

model.myDate = string.IsNullOrEmpty(a) ? null : DateTime.Parse(a);

Do you return the model to the view properly?

  • See the edit I made in bold please. The problem isn't the orientation of the date, but the date literally not displaying. – tonyleMill Feb 09 '16 at 18:34