0

I'm finally able to use a date picker after a long fight but it's not over...

My problem is : I want the date picker showing the format dd/mm/yy to the clients so I putted this script :

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

And i'm calling it with this ( to modify my date attribut in my object) :

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

The problem is, when I tried to submit this, it doesn't pass but when I use this script ( without dateFormat ) everything works perfectly :

<script>
    $(document).ready(function() {
        $('.date').datepicker();
    });
</script>

So my question is, is there a way to change the view of the date for the client but not the content that's send ?

Thanks in advance !

MrPixel6
  • 357
  • 1
  • 4
  • 19
  • 1
    and what Date format you need on server side? – teo van kot Mar 17 '16 at 08:01
  • 1
    The correct use is `@Html.TextBoxFor(m => m.date, "{0:dd/MM/yyyy}", new { @class = "date" })` - never try and set the `value` attribute when using the HtmlHelper methods. Set the value of `date` in the controller before you pass the model to the view –  Mar 17 '16 at 08:02
  • @teovankot like you see, I don't really get anything with those date once it's working in dd-mm-yy and an other time it's working with mm-dd-yy... In my db it's in dd-mm-yy but it's seems that's only working when I send mm-dd-yy format... I really don't get it... – MrPixel6 Mar 17 '16 at 08:10
  • @StephenMuecke I tried it this way but it's not working :( – MrPixel6 Mar 17 '16 at 08:11
  • I was commenting on your incorrect usage of the helper :) You need to let us know what the server side culture is as @teovankot asked. And if its working with `MM/dd/yyyy` then clearly your culture is not one which accepts dates in `dd/MM/yyyy` format. –  Mar 17 '16 at 08:12
  • @StephenMuecke Ho ok ^^ In my Web.config, I have but this line – MrPixel6 Mar 17 '16 at 08:15
  • Then change it to (say) `` (Australia, which has dates in `dd/MM/yyyy` ) and it will work. Although if you also have client side validation, you have another issue as well. –  Mar 17 '16 at 08:17
  • I tried it and remove all client side validation and it's still doesn't want to pass :s – MrPixel6 Mar 17 '16 at 08:24
  • If your issue was client side validation, then refer [this answer](http://stackoverflow.com/questions/27285458/jquery-ui-datepicker-and-mvc-view-model-type-datetime/27286969#27286969) –  Mar 17 '16 at 08:35
  • @StephenMuecke Just tried it, When I put a date with 1 digit for the day ( like 09-03-2016) I got an immediate error in client side validation and when I put a 2 days digits ( like 21-03-2016), I got an server side validation error ... – MrPixel6 Mar 17 '16 at 08:46
  • You have specified it as `dd/MM/yyyy` so it needs to be `/` not `-` –  Mar 17 '16 at 08:47
  • @StephenMuecke Well Well Well... now It's working with dd/MM/yy but not with dd/mm/yy ^^' It's not quite what I wanted but it's working :p So Thank you very much ! – MrPixel6 Mar 17 '16 at 08:54
  • In the datepicker initialization it still needs to be `dateFormat: dd/mm/yyyy` (not `MM`). I was referring to the fact you were entering `09-03-2016` when you need to enter `09/03/2016` –  Mar 17 '16 at 08:58
  • @StephenMuecke Right ! Thank you ! – MrPixel6 Mar 17 '16 at 08:59

1 Answers1

0

Thanks to @StephenMuecke ! The answer is :

Script :

<script>
    $(document).ready(function() {
        $('.date').datepicker({ dateFormat: 'dd/mm/yy'});
    });
    $.validator.addMethod('date', function (value, element) {
        if (this.optional(element)) {
            return true;
        }
        var valid = true;
        try {
            $.datepicker.parseDate('dd/MM/yy', value);
        }
        catch (err) {
            err.description;
            valid = false;
        }
        return valid;
    });
</script>
  • Put your culture in the web.config file
MrPixel6
  • 357
  • 1
  • 4
  • 19