0

I am using the following for displaying the date field in my razor view form:

@Html.TextBoxFor(m => m.LectureDate, "{0:dd/MM/yyyy}")

Which should allow the date format to be displayed in format 21/04/2017, which is what is being supplied through JQuery DatePicker, but after it is set to this by datepicker and the form is submitted, the MVC Validation throws error and does not allow to submit the form.

I have tried applying <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB" /> in Web.Config. I have tried adding the data annotations attributes specified in the answers suggested.

And I have also tried specifying the format in the Html Helper only. But, still the MVC throws error and does not submit the form.

How can this be done?

teenup
  • 7,459
  • 13
  • 63
  • 122
  • can you show the error message and what data annotations you used to validate – Usman Apr 11 '17 at 15:14
  • you can check this http://stackoverflow.com/a/28356228/5671377 – Usman Apr 11 '17 at 15:16
  • Passing the format there only affects the display, not what is expected on postback. Your need to use `DisplayFormat` with `ApplyFormatInEditMode=true`. – Chris Pratt Apr 11 '17 at 15:20
  • I am using nothing in the Data Annotation. Getting the validation error - The field must be a date. – teenup Apr 11 '17 at 15:26
  • If the form is not submitting, its a `jquery.validate` issue (which validates dates based on `MM/dd/yyyy` format). You need to reconfigure the `$.validator`. What datepicker plugin are you using? –  Apr 11 '17 at 22:17
  • @StephenMuecke You are right. It was JQuery Validate issue. I was able to resolve it by reconfiguring the $.validator. Thanks. – teenup Apr 12 '17 at 07:31

1 Answers1

1

Just add the following code in your web.config file

<system.web>
  <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB" />
</system.web>

then your date format should appear like (dd/MM/yyyy)

also use

@Html.EditorFor(m => m.LectureDate)
Mzhda Saeed
  • 213
  • 2
  • 4