1

In an asp.net MVC application, I have the following property:

...
public System.DateTime StartTime { get; set; }
...

And, in a view (in a form):

<div class="form-group">
   @Html.LabelFor(model => model.StartTime, htmlAttributes: new { @class = "control-label col-md-2" })
   <div class="col-md-10">
      @Html.EditorFor(model => model.StartTime, new { htmlAttributes = new { @class = "form-control" } })
      @Html.ValidationMessageFor(model => model.StartTime, "", new { @class = "text-danger" })
   </div>
</div>

in the javascript:

$("#StartTime").datepicker({
    dateFormat: "dd/mm/yy",
});

I get the following error with the default jquery validation of asp.net MVC:

The field StartTime must be a date.

I can handle the date in dd/MM/YYYY format without any error in asp.net MVC?

2
  • Is the last sentence a question or a statement? Commented Dec 14, 2015 at 15:43
  • 1
    Your error is occurring either because your server culture does not accept dates in the format dd/MM/yyyy or because you have not modified the jQuery validator (by default it will validate dates based in MM/dd/yyyy). Refer this answer for some options Commented Dec 14, 2015 at 21:54

1 Answer 1

0

I had the same problem, and the key of the issue was the dateFormat. If you use default format there is no problem when submitting, but if you change it then there are problems submitting it.

The solution I found was to use altField and altFormat. With datepicker you can have a format for the showing value and an alternative hidden field (altField) with a different format (altFormat). To use it do something like:

    $("#StartTime").datepicker({
    dateFormat: "dd/mm/yy",
    altField: $(this).val(),
    altFormat: "M/dd/yy"
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.