【发布时间】:2015-05-20 10:07:39
【问题描述】:
我指的是以下链接: stackoverflow.com/questions/18288675/display-datetime-value-in-dd-mm-yyyy-format-in-mvc4
你的模型
[Required(ErrorMessage = "Enter the Issued date.")]
[DataType(DataType.Date)]
public DateTime IssueDate { get; set; }
剃刀页面
@Html.TextBoxFor(model => model.IssueDate)
@Html.ValidationMessageFor(model => model.IssueDate)
Jquery DatePickter
$(document).ready(function () {
$('#IssueDate').datepicker({
dateFormat: "dd/mm/yy",
showStatus: true,
showWeeks: true,
currentText: 'Now',
autoSize: true,
gotoCurrent: true,
showAnim: 'blind',
highlightWeek: true
});
});
网络配置文件
<system.web>
<globalization uiCulture="en" culture="en-GB"/>
</system.web>
它在使用日期格式 dd/MM/yyyy (例如 3/5/2015)时有效,但在使用日期格式(例如 29/5/2015)时无效。
一旦我们从 jquery datepicker 中选择日期,文本框将显示 29/5/2015。只是当它回发到服务器时,模型将显示日期为空(我正在使用 DateTime?)。它会在 2015 年 3 月 5 日正常工作...
有什么帮助吗?
编辑:
我将以下内容添加到我的控制器中。当我回发时,下面的函数将执行并显示 en-GB。然后它将进入我假设处理httppost的另一个动作,但它显示为en-US!
protected override void Initialize(System.Web.Routing.RequestContext requestContext)
{
base.Initialize(requestContext);
const string culture = "en-GB";
CultureInfo ci = CultureInfo.GetCultureInfo(culture);
Thread.CurrentThread.CurrentCulture = ci;
Thread.CurrentThread.CurrentUICulture = ci;
}
【问题讨论】:
-
出于兴趣,如果您尝试 2015 年 5 月 29 日会发生什么?
-
^Steve,是的,它可以接受...
-
那么您的日期文化有问题,它试图将其解析为 29/5/2015 失败的美国日期 (mm/dd/yy)。您需要查看您的文化设置。
-
我在下面添加到我的控制器。当我回发时,下面的函数将执行并显示 en-GB。然后它将进入我假设处理httppost的另一个动作,但它显示为en-US! 受保护的覆盖 void Initialize(System.Web.Routing.RequestContext requestContext) { base.Initialize(requestContext);常量字符串文化=“en-GB”; CultureInfo ci = CultureInfo.GetCultureInfo(culture); Thread.CurrentThread.CurrentCulture = ci; Thread.CurrentThread.CurrentUICulture = ci; }
-
^Steve,我再次检查了我的代码,似乎在 BeginExecuteCore 中,我将文化设置为“en-US”。我改变了它,现在我的 Action 方法正在以正确的文化和代码工作的方式执行。感谢您的评论,它改变了我对问题根源的看法。如果你给它一个答案,我很乐意接受它......
标签: c# jquery asp.net-mvc asp.net-mvc-4