* *更新
我刚刚意识到日期在发布期间根本没有数据绑定,除非我使用 Firefox。
为什么浏览器会影响服务器端的数据绑定?
我觉得我正在服用疯狂的药丸
我的问题是我有一个 JQuery 日期选择器,它的格式似乎正确(mm/dd/yyyy),但是当值被发布到服务器时,月份和日期会被切换。
date 属性像这样添加到页面中(它被隐藏了,以便在发布期间可以绑定。如果它是一个简单的文本框,问题也是一样的):
<input type="hidden" name="SearchByDate" id="SearchByDate" value="<%: ((DateTime)Model.SearchByDate).ToString(Web.Common.Config.GetDateFormatBase) %>" />
在我的视图模型中,我指定了这样的日期格式:
[DisplayFormat(DataFormatString = Common.Config.GetDateFormat,
ApplyFormatInEditMode = true)]
public DateTime? SearchByDate { get; set; }
我的 javascript datepicker 的格式如下:
$("#appointmentDateDiv").datepicker({
------> dateFormat: "<%= Web.Common.Config.GetDateFormatJavascript %>",
changeMonth: true,
onSelect: function (dateText, inst) {
$("#SearchByDate").val(dateText);
$("form").submit();
}
});
我实际上在这样的公共类中对格式进行了规范:
public const string GetDateFormatBase = @"MM/dd/yyyy";
public const string GetDateFormat = @"{0:" + GetDateFormatBase + "}"; //This is for annotations
public const string GetDateFormatJavascript = "mm/dd/yy";
如您所见,格式都是 sam:month day year。当我第一次加载这样的页面时,一切看起来都很好,所以日期选择器让我可以选择正确格式的日期。然后我发布,如果我单步执行我的代码,一切看起来仍然很好。我什至重新格式化日期以确保在返回视图之前。但是月份和日期总是会切换。
最奇怪的是,在 Firefox 中一切正常。它在 IE、Chrome 和 Opera 中失败。但我不明白为什么...