0

So, It's not the first time I ask that kind of question but it's still not working...

I've put a date picker like this :

<script>
    $(document).ready(function () {
        $('.date').datepicker({ dateFormat: "dd/mm/yy", firstDay: 1 });
    });
</script>

And I call it this way :

@Html.TextBox("date", null, new { @class = "date", @Value = DateTime.Today.Date })

The problem is : as you see I'd like to have a dd/mm/yy format and that's the case when I select a date, it's in dd/mm/yy but when I push the submit button, it's not going to the controller and the datepicker shows up like to say : "Hey you made a mistake, I want a mm/dd/yy format"...

I've tried to remove the client side validation with @{ Html.EnableClientValidation(false); } but it doesn't change anything....

Can please someone help me ? Thanks in advance !

EDIT : Like I said, maybe not clearly enought, the problem is not coming from the server but from the client ! I have disable client validation in my web.config and it's now working

MrPixel6
  • 357
  • 1
  • 4
  • 19
  • did you try setting the `dateFormat` to `d/m/yy`? – Lulylulu Apr 15 '16 at 15:05
  • @Lulylulu just tried it, not better :( – MrPixel6 Apr 15 '16 at 15:08
  • which datepicker you use ? – Lulylulu Apr 15 '16 at 15:15
  • @Lulylulu Jquery datepicker – MrPixel6 Apr 15 '16 at 15:17
  • but which version ? the problem is that I tested the code and for me it works. Try putting `@Value = DateTime.Today.ToString("dd/MM/yyyy")` . The `.Date` has no effect because `.Today` already puts the time at `00:00:00` – Lulylulu Apr 15 '16 at 15:23
  • @Lulylulu I'm using the jQuery UI 1.11 and the problem is not coming from the Value, I cant put value to "null" it's ok... it's just when I put a Date where days are bigger than 12 it's not sending to POST method in my controller and if the days are under or equals to 12, it convert the date in mm/dd/yy (days become months) – MrPixel6 Apr 15 '16 at 15:33
  • 1
    Because you have different culture on the server than the machine. For example, you server maybe it's configured to accept mm/dd/yyyy (en-US) and your machine (localhost) it's es-MX (dd/mm/yyyy) so it will show the message that the date is not valid. Add this to the web.config `` so the server will accept dd/mm/yyyy – Jorge F Apr 15 '16 at 18:19
  • If you had actually put the real error message in your question, it would have obvious. Change you server culture or create a custom model binder –  Apr 15 '16 at 22:18

1 Answers1

0

What does your attributes look like for the Date property in your model? I experienced a problem similar to yours not too long ago. You want to debug and figure out what the current system date format is.

You could try inserting a simple function into your .datepicker function like so:

$(".date").datepicker({
    dateFormat: "dd-mm-yy",
    onSelect: function () {
        var selectedDate = $('#the-id-of-my-datepicker-goes-here').datepicker('getDate');
        console.log("The current selected date is: " + selectedDate); //alternatively alert(selectedDate);
    }
})
Sean
  • 507
  • 1
  • 9
  • 27
  • I've tried your solution to see the format and its mm/dd/yy so how can I make it work to be seen as a good format when it's dd/mm/yy ? Because I don't understand the point to put a "dateFormat" attribut if it's just for the view and it's not smart enought for validation... – MrPixel6 Apr 18 '16 at 15:39
  • @MrPixel6 The `dateFormat` attribute is for the front end - how your datepicker will be showing it's value to the user. If you need it for validation, you will simply need to add a `DataType` attribute like so: `[DataType(DataType.Date)]` to your `DateTime` model property. You can also specify the format for the back end here using `[DisplayFormat(DataFormatString = "{0:YYYY-mm-dd}")]` where your format will replace :"YYYY-mm-dd". Hope that helps. – Sean Apr 18 '16 at 15:43
  • `[DataType(DataType.Date)]` has absolutely nothing to do with validation (its sole purpose is to generate `type="date"` when using the `EditorFor()` method) –  Apr 18 '16 at 23:42
  • @StephenMuecke first I was working with Editor For on Chrome and it was perfect but then I tried on safari and firefox and it doesn't work on these... – MrPixel6 Apr 19 '16 at 07:06
  • @MrPixel6, Thats because `type="date"` (which reneders the browsers HTML-5 datepicker) is only supported in Chrome (recent versions) and Edge –  Apr 19 '16 at 07:20
  • @StephenMuecke Ho great ^^' And what do you do if you want something that work on every browser?.... That's what I'm desperately looking for... – MrPixel6 Apr 19 '16 at 07:34
  • @MrPixel6, Then use a jquery datepicker plugin as your currently doing. –  Apr 19 '16 at 07:36
  • @StephenMuecke And that's exactly what my question is ^^ how are you doing to make it work because only date in mm/dd/yy are passing – MrPixel6 Apr 19 '16 at 08:11
  • @MrPixel6, Instead of _"Hey you made a mistake, I want a mm/dd/yy format".._ Add your real error message. And read the last 2 comments to your question –  Apr 19 '16 at 08:13
  • @StephenMuecke There is no error message that's why... My date picker is just opening again when i push "submit" and I've already a globalization culture in my web.config it's not a server side problem it's client side – MrPixel6 Apr 19 '16 at 08:19
  • 1
    @MrPixel6, Its not a client side validation issue if you have disabled client side validation. But if you do want client side validation, then you need to configure the jquery validator. Refer [this answer](http://stackoverflow.com/questions/27285458/jquery-ui-datepicker-and-mvc-view-model-type-datetime/27286969#27286969). –  Apr 19 '16 at 08:22
  • @StephenMuecke Thank you ! I had already tried it but I've placed it wrong ! That is what I was looking for ! Thank You ! – MrPixel6 Apr 19 '16 at 08:52