0

I have researched this issue extensively and still can't find an answer. I have a model with this date field:

[Display(Name = "Start Date")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}", NullDisplayText = "")]
[Required]
public Nullable<System.DateTime> ConsumptionDate;

I have a view with this EditorFor:

@Html.EditorFor(model => model.ConsumptionDate)
@Html.ValidationMessageFor(model => model.ConsumptionDate, "*", new { @class = "text-danger" })

But the date still never gets formatted. I have tried with and without "{0:d}" as the format in the EditorFor, and I have tried with and without [DataType(DataType.Date)] annotated on the model field. Any ideas what I'm doing wrong? The binding works fine and I can save the data fine too - I just can't get it to format properly.

TIA -VG

VG1
  • 185
  • 9
  • 2
    try this format, this is what I use: `[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]` – DLeh Mar 06 '15 at 18:54
  • I have tried "MM-dd-yyyy" format as well, but the format never gets applied regardless of which date format string I am trying. – VG1 Mar 06 '15 at 19:02
  • the one you just mentioned is different from the one i posted, you should post all the formats you have tried so far. – DLeh Mar 06 '15 at 19:03
  • I don't want the year to display first so I wouldn't use the format you posted. I only tried the one other format. "d" is what I really need tho, since that will take culture into consideration when formatting. – VG1 Mar 06 '15 at 19:09
  • since you're using an `EditorFor()` with a `DateTime`, the rendered output should be ``, which would make the *browser* responsible for figuring out how to display that. for instance, Chrome has a date editor control. In order for the input to recognize the string as a date, you will need to format it how I suggested. If you want to choose how the date is displayed, you should use `TextBoxFor()` instead of `EditorFor()` – DLeh Mar 06 '15 at 19:12
  • I can get it to work with TextboxFor, yes, but all the advice on MVC indicates I _should_ be using EditorFor and indicating that all you need on an EditorFor is the DisplayFormat attribute on the model field. So my question is, why is EditorFor not working? – VG1 Mar 06 '15 at 19:15
  • Also, I don't understand what you mean by "In order for the input to recognize the string as a date, you will need to format it how I suggested". How will formatting it with the year first help in any way? Especially since that's not the format I need? – VG1 Mar 06 '15 at 19:16
  • because you're not formatting properly, try formatting it like i said, you should get a value in. like i said, this will render as an `` which expects a certain string format in order to parse the string into a date properly, which is the one i specified. also, try adding `[DataType(DataType.Date)]` – DLeh Mar 06 '15 at 19:16
  • Fine, I just tried your format as well. Still didn't work. In my original post I point out that I've already tried [DataType(DataType.Date)] – VG1 Mar 06 '15 at 19:20
  • Have you tried to remove "datePickerField" class? – Deilan Mar 06 '15 at 19:21
  • http://stackoverflow.com/a/12634470/526704 – DLeh Mar 06 '15 at 19:21
  • possible duplicate of [MVC4 DataType.Date EditorFor won't display date value in Chrome, fine in Internet Explorer](http://stackoverflow.com/questions/12633471/mvc4-datatype-date-editorfor-wont-display-date-value-in-chrome-fine-in-interne) – DLeh Mar 06 '15 at 19:21
  • Not a duplicate - this has nothing to do with Chrome, and the display is fine, it just won't format. You are starting to get off-topic. – VG1 Mar 06 '15 at 19:23
  • If I remove the datePickerField class the datepicker stops working AND the date is still not formatted properly. – VG1 Mar 06 '15 at 19:23
  • There's no such thing as a "format" for editorFor so of course "{0:d}" does nothing. If you want a format applied then use `Html.TextboxFor(...)`. – jamesSampica Mar 06 '15 at 20:11
  • Hi @Shoe, I do see that EditorFor doesn't take a format field - that was a desperation move. But the documentation indicates the data attribute on the model should be used and it's not formatting it either. – VG1 Mar 06 '15 at 20:28
  • Are you using a jquery datepicker plugin or are you wanting to display the HTML5 datepicker implementation of the browser? –  Mar 06 '15 at 21:59
  • The datepicker is not important at this time. I've removed that code from the above post to prevent distraction. The problem occurs regardless of whether the field uses a datepicker or not. – VG1 Mar 10 '15 at 12:49

1 Answers1

0

Did you use System.DateTime with or without the questionmark (indicating that the editor can accept nullable datetimes?

@model System.DateTime?
Atron Seige
  • 2,783
  • 4
  • 32
  • 39