开发者

JQuery Datepicker datetime issue parsing to @Html.EditorFor MVC3

I've read a lot of issues about setting the correct datetime but can't get it right.

My MVC3 approach is database first. The model is auto generated and manually changed;

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yy}")]
    public Nullable<System.DateTime> Datum { get; set; }

in my view it's a straight forward razor;

    <div class="editor-field">
        @Html.EditorFor(model => model.Datum)
        @Html.ValidationMessageFor(model => model.Datum)
    </div>

in Shared_Layout I added this

jQuery(function ($) {

$.datepicker.regional['nl'] = {
            closeText: 'Sluiten',
            prevText: '←',
            nextText: '→',
            currentText: 'Vandaag',
            monthNames: ['januari', 'februari', 'maart', 'april', 'mei', 'juni',
                'juli', 'augustus', 'september', 'oktober', 'november', 'december'],
            monthNamesShort: ['jan', 'feb', 'maa', 'apr', 'mei', 'jun',
                'jul', 'aug', 'sep', 'okt', 'nov', 'dec'],
            dayNames: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'v开发者_运维知识库rijdag', 'zaterdag'],
            dayNamesShort: ['zon', 'maa', 'din', 'woe', 'don', 'vri', 'zat'],
            dayNamesMin: ['zo', 'ma', 'di', 'wo', 'do', 'vr', 'za'],
            weekHeader: 'Wk',
            dateFormat: 'dd-mm-yy',
            firstDay: 1,
            isRTL: false,
            showMonthAfterYear: false,
            yearSuffix: ''
        };

        $.datepicker.setDefaults($.datepicker.regional['nl']);
        $.datepicker.setDefaults({ dateFormat: 'dd-mm-yy' });
    });

In the EditorFor field the date is presented dd-mm-yyyy The datepicker is format correctly in dd-mm-yy After parsing the date from the datepicker, dateformat is suddenly mm/dd/yyyy


Found out the issue... I forgot I used a shared template for date :-/

@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : string.Empty), new { @class = "datepicker" })


<script type="text/javascript">
    $(document).ready(function () {
        $('.datepicker').datepicker({ dateFormat: "dd-mm-yy" });
    });
</script>

regards, Roy

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜