日期格式 EN-AU 不接受 dd/mm/yyyy Client-side 和 Server-Side

Date Format EN-AU Not accepting dd/mm/yyyy Client-side and Server-Side

我有几个页面为我的视图模型日期时间属性使用 jquery UI 日期选择器。 这是我的视图模型:

public class UsageFilterViewModel : BaseModel
{
    [Display(Name="Entity Name")]
    public string EntityName { get; set; }
    public string UserLogin { get; set; }
    public int[] SelectedQuestionGroup { get; set; }
    public IEnumerable<SelectListItem> QuestionGroups { get; set; }
    [Display(Name = "Date From"), DataType(DataType.DateTime)]
    public DateTime? DateFrom { get; set; }
    [Display(Name = "Date To"), DataType(DataType.DateTime)]
    public DateTime? DateTo { get; set; }
    [Display(Name="Exclude IP")]
    public string ExcludeIP { get; set; }
    public string URL { get; set; }
}

我的看法:

@using (Html.BeginForm(null, null, FormMethod.Get, new { @class = "form-horizontal", @id = "formfilter" }))
{
    @Html.AntiForgeryToken()
    <input type="hidden" value="True" name="Display" />
    <table class="table table-condensed table-responsive table-hover smart-form">
    <tr>
        <th>Entity Name</th>
        <td>@Html.TextBoxFor(m => m.EntityName, new { @style = "width: 75px" })</td>
        <th>User Login</th>
        <td>@Html.TextBoxFor(m => m.UserLogin, new { @style = "width: 75px" })</td>
        <th>Question Group</th>
        <td>@Html.ListBoxFor(m => m.SelectedQuestionGroup, Model.QuestionGroups, new { @class = "select2", @multiple = "multiple" })</td>
    </tr>
    <tr>
         <th>From</th>
         <td>
             <div class="input-group" style="width:100px;">
             @Html.TextBoxFor(m=>m.DateFrom, Model.DateFrom.HasValue ? Model.DateFrom.Value.ToShortDateString() : null, new { @placeholder = "From", @style = "width: 75px" })
             <span class="input-group-addon"><i class="fa fa-calendar"></i></span>

             @Html.TextBoxFor(m=>m.DateTo, Model.DateTo.HasValue ? Model.DateTo.Value.ToShortDateString() : null, new { @placeholder = "To", @style = "width: 75px" })
              <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
              </div>
          </td>
          <th>Exclude IP</th>
          <td>@Html.TextBoxFor(m => m.ExcludeIP, new { @style = "width: 75px" })</td>
          <th>URL</th>
          <td>@Html.TextBoxFor(m => m.URL, new { @style = "width: 75px" })</td>
      </tr>
   </table>
   <div class="widget-footer">
       @Html.ValidationSummary(false, null, new { @class = "alert alert-warning fade in" })
       <button class="btn btn-info" type="submit">Display</button>
   </div>
}

创建日期选择器的 javascript:

$(document).ready(function () {
    $("#DateFrom").datepicker({
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 3,
        dateFormat: 'dd/mm/yy',
        prevText: '<i class="fa fa-chevron-left"></i>',
        nextText: '<i class="fa fa-chevron-right"></i>',
        onClose: function (selectedDate) {
            $("#DateTo").datepicker("option", "minDate", selectedDate);
        }

    });
    $("#DateTo").datepicker({
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 3,
        dateFormat: 'dd/mm/yy',
        prevText: '<i class="fa fa-chevron-left"></i>',
        nextText: '<i class="fa fa-chevron-right"></i>',
        onClose: function (selectedDate) {
            $("#DateFrom").datepicker("option", "maxDate", selectedDate);
        }
    });
});

关于 client-side:当我输入诸如“21/04/2015”之类的日期时,验证通过,并且日期选择器正在创建日期正确的格式。因此页面正在回发。

关于server-side:验证失败。

ModelState.IsValid = false

当视图呈现时,Html.ValidationSummary 呈现这个:

The value '20/04/2015' is not valid for Date From. The value '21/04/2015' is not valid for Date To.

根据 this explanation,我的 Web.Config 明确将文化设置为 EN-AU:

<system.web>
    <globalization uiCulture="en-AU" culture="en-AU" enableClientBasedCulture="false" />
</system.web>

我试过this solution,但结果一样。

HTTP 请求的 request-headers 是:

Accept-Language:en-GB,en;q=0.8

我可以通过覆盖 jquery ui 日期格式来使 client-side 工作:

$.validator.methods.date = function (value, element) {
    var dateRegex = /^(0?[1-9]\/|[12]\d\/|3[01]\/){2}(19|20)\d\d$/;
    return this.optional(element) || dateRegex.test(value);
};

此外,

@System.Threading.Thread.CurrentThread.CurrentCulture returns "en-AU"

似乎通过在 Web.config 中设置区域性,它应该接受日期、货币等格式。

我怎样才能让它同时适用于 dd/mm/yyyy 客户端和 server-side?

我将来也会引入全球化,所以我希望解决方案可以为其他语言环境做好准备。

有两个变化:

  1. en-au日期格式为:dd/MM/yyyy(注意MM)。为模型中的数据字段指定以下属性。

    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

  2. 在 jquery 日期选择器上,将日期格式更改为 'dd/MM/yyyy' 否则它将以两位数发送年份。

这个问题的解决方案是使用 HTTP POST 方法而不是 GET,按照:this answer。 它不是一个理想的解决方案,因为有时我想使用 GET 方法提交表单以提交回相同的操作。

我想覆盖 GetHttpHandler like this,但不在路由中使用和显示任何内容。