Chrome

时间:2018-05-08 11:04:15

标签: c# asp.net-mvc date

我正在构建我的第一个应用程序,但现在我正处于Chrome处理日期的方式(与IE结合使用)。

我有一个带有字段的模型:

[Display(Name = "AcceptanceDate", ResourceType = typeof(Resources.ModelResources))]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")]
        public DateTime? AcceptanceDate { get; set; }

我在视图中使用此字段。

     @if (Request.UserAgent.ToLower().IndexOf("chrome") > -1)
                            {
 @Html.EditorFor(m => m.AcceptanceDate, ViewBag.disableControls == true ? (object)new { disabled = "disabled", @class = "form-control" } : new { @class = "form-control", @type = "date" })
                            }
                            else
                            {
                                @Html.EditorFor(m => m.AcceptanceDate, ViewBag.disableControls == true ? (object)new { disabled = "disabled", @class = "form-control", @Value = Model.AcceptanceDate.HasValue ? Model.AcceptanceDate.Value.ToString("dd-MM-yyyy") : null } : new { @class = "form-control", @type = "date", @Value = Model.AcceptanceDate.HasValue ? Model.AcceptanceDate.Value.ToString("dd-MM-yyyy") : null })
                            }

使用以下代码在字段上添加日期选择器:

<!-- jQueryUI Stylesheet (from CDN) -->
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/smoothness/jquery-ui.min.css" rel="stylesheet" type="text/css" />

<script type='text/javascript'>
    $.datepicker.regional['nl'] = {
        monthNames: [
            'Januari', 'Februari', 'Maart', 'April', 'Mei', 'Juni', 'Juli', 'Augustus', 'September', 'Oktober',
            'November', 'December'
        ],
        monthNamesShort: ['Jan', 'Feb', 'Mar', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'],
        dayNames: ['Maandag', 'Dinsdag', 'Woensdag', 'Donderdag', 'Vrijdag', 'Zaterdag', 'Zondag'],
        dayNamesShort: ['Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za', 'Zo'],
        dayNamesMin: ['Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za', 'Zo']
    }

    $.datepicker.setDefaults($.datepicker.regional['nl']);
    $(function () {
        $("input[type='date']").datepicker({
            dateFormat: 'dd-mm-yy',
            showButtonPanel: false
        }).attr('type', 'text');
    });
</script>

dateformat设置为dd-MM-yyyy。当我在Internet Explorer中调用视图时,一切都很好。该值从数据库中检索并显示为预期(DD-MM-YYYY),日期选择器的工作方式类似于魅力(所选日期也显示为DD-MM-YYYY),保存数据也有效。

但是当我使用chrome时,初始fieldvalue(加载视图时)显示为YYYY-MM-DD。日期选择器确实有效,并且在选择不同的日期之后,日期被选为DD-MM-YYYY。

有人可以告诉我为什么chrome显示初始值为YYYY-MM-DD ?? 我尝试在视图中将EditorFor的代码更改为以下代码:

@Html.EditorFor(m => m.AcceptanceDate, ViewBag.disableControls == true ? (object)new { disabled = "disabled", @class = "form-control", @Value = Model.AcceptanceDate.HasValue ? Model.AcceptanceDate.Value.ToString("dd-MM-yyyy") : null } : new { @class = "form-control", @type = "date", @Value = Model.AcceptanceDate.HasValue ? Model.AcceptanceDate.Value.ToString("dd-MM-yyyy") : null })

但是这导致文本框中没有显示任何值。 (该值已发送到视图但未显示在控件中。

请保持警惕。

0 个答案:

没有答案