4

I'm running into an issue on a clients production server where the month & day is being swapped on a view model property when saving an entity. For example if I were to select April 9th 2012 (04/09/2012) within the datepicker the date is being saved as September 4th 2012 (09/04/2012).

The same controller actions are working correctly on local development / test server and I'm unsure of what to look at next?

The property in the viewmodel:

[DisplayName("Date")]
[Required]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yy}")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }

The helper within the relevant view:

@Html.EditorFor(model => model.StartDate)

The jquery ui datepicker:

$('#StartDate').datepicker({ dateFormat: 'mm/dd/yy' });

The stripped down version of the controller:

public ActionResult Create(BulletinBoardViewModel bulletin)
{
    var model = AutoMapper.Mapper.Map<BulletinBoardViewModel, BulletinBoard>(bulletin);
    _repository.Save(model);

    return RedirectToAction("Index");
}

The headers of the related post:

StartDate:04/09/2012

Cache-Control:private, s-maxage=0
Content-Length:131
Content-Type:text/html; charset=utf-8
Date:Tue, 10 Apr 2012 02:43:16 GMT
Location:/BulletinBoard
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-AspNetMvc-Version:3.0
X-Powered-By:ASP.NET
4

1 回答 1

2

听起来客户端的服务器设置为英国英语而不是美国。这是一个常见的 CultureInfo 问题。无论哪种方式,当对象被持久化时,日期值应该仍然是正确的。由于 UI 文化选择,它只是以不同的方式显示。

你能识别出这是一个实际问题的特定代码,而不仅仅是一个明显的问题吗?

此外,它实际上并没有被保存为 9 月 4 日。你就是这么读的。尝试挑选,例如,12 月 31 日。保存实体后,您可能会看到 2012 年 12 月 31 日。服务器只是以 dd/MM/yyyy 格式显示日期。

于 2012-04-10T03:49:51.597 回答