0

I have a form which has a date field. I use JQuery datepicker for it. This doesn't give any validation error on client side but I get a validation error when trying to update database.

The error is "The value '27/09/2012' is not valid for AvailableFrom."

My entity is as follows - generated by code first:

[DisplayFormat(DataFormatString = "{0:dd/mm/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? AvailableFrom { get; set; }

JQuery Script

<script type="text/javascript" src="@Url.Content("~/Scripts/jquery-ui-1.8.23.datepicker.min.js")"></script>
<script>
    $(function () {
        $('.datepicker').datepicker({ minDate: 0, dateFormat: 'dd/mm/yy' });
    });
    </script>

View:

<div class="editor-label">
    @Html.LabelFor(model => model.Property.AvailableFrom)
    @Html.TextBoxFor(model => model.Property.AvailableFrom, new { @class = "datepicker" })
</div>
<div class="editor-field">            
    @Html.ValidationMessageFor(model => model.Property.AvailableFrom)
</div>
Tripping
  • 919
  • 4
  • 18
  • 36
  • The error message has nothing to do with JQuery DatePicker. Your database is not accepting the value Try changing the date format so that both have identical format (either yy or yyyy) – Bishnu Paudel Sep 23 '12 at 11:42
  • May be I was wrong, Have a look at this answer. http://stackoverflow.com/questions/7835614/asp-net-mvc3-datetime-format – Bishnu Paudel Sep 23 '12 at 11:48
  • Isn't yy in JQuery return "2012". So, yyyy in database should match with it – Tripping Sep 23 '12 at 11:48

1 Answers1

1

Works perfectly after adding globalisation to web.config under

<configuration>
   <system.web>
     <globalization uiCulture="en-GB" culture="en-GB"/>
     ...

See MSDN page here: http://msdn.microsoft.com/en-us/library/ydkak5b9(v=vs.71).aspx

Tripping
  • 919
  • 4
  • 18
  • 36