-1

So I have this property in my model:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy hh:mm}")]
[DataType(DataType.DateTime)]
public DateTime EventDate { set; get; }

I'm using a vuejs component for the datetime picker, so when the form is submitted the EventDate value looks like: "21-11-2017 20:47".

However on server side, in the controller action my model is always invalid: The value '21-11-2017 20:47' is not valid for EventDate.

Any idea what is wrong here? I've been trying different variations like using DateType.Date or other format strings, but the result was the same

[Update] The answers here suggest this is a duplicate of this question here

Although the other question is not the same, the last part of the answer there suggests what could be wrong in my case. It basically says that DisplaFormat with DataFormatString is ignored by model binder and I should either:

  • create a custom model binder to make mvc understand my custom format for EventDate
  • or use a standard date format that mvc understands by default

I guess there is no attribute yet to instruct mvc how to parse a date string during model binding, which is what I was hoping for..

Andrei
  • 31
  • 1
  • 6
  • 2
    Duplicate of https://stackoverflow.com/questions/13253964/displayformat-applyformatineditmode ? – Dylan Nicholson Nov 26 '17 at 19:59
  • 1
    Possible duplicate of [DisplayFormat ApplyFormatInEditMode](https://stackoverflow.com/questions/13253964/displayformat-applyformatineditmode) – Camilo Terevinto Nov 26 '17 at 20:22
  • You need to change to culture in the server to one that matches that date format (or create a custom ModelBinder to bind your dates) –  Nov 26 '17 at 20:27

1 Answers1

0

I had Same issue I fix it by copying this code in my web.config file you should also try it once see if it help you

<system.web>
<globalization uiCulture="en-AU" culture="en-AU" />
</system.web>
Mohammad Edris Raufi
  • 1,393
  • 1
  • 13
  • 34