日期格式EN-AU不接受dd/mm/yyyy客户端和服务器端



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

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);
}
});
});

在客户端:当我输入日期(如"2015年4月21日")时,验证通过,并且日期选择器正在以正确的格式创建日期。因此,该页面正在回发。

在服务器端:验证失败。

ModelState.IsValid = false

当视图渲染时,Html。ValidationSummary表示:

值"20/04/2015"对"日期起始"无效。价值"2015年4月21日"对于截止日期无效

根据这个解释,我的网站。Config显式地将区域性设置为EN-AU:

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

我尝试过这个解决方案,但结果是一样的。

HTTP请求的请求标头为:

接受语言:en-GB,en;q=0.8

我可以通过重写jquery ui日期格式来让客户端工作:

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

此外,

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

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

如何让它在dd/mm/yyyy客户端和服务器端都能工作

我还将在未来引入全球化,所以我希望该解决方案能为其他地区做好准备。

有两个变化:

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

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

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

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

我想像这样覆盖GetHttpHandler,但不使用和显示Routes中的任何内容。

最新更新