首页 文章

Asp.net日期时间文化问题

提问于
浏览
0

我在asp.net中使用 datetime 时遇到了一个问题 . 在根据土耳其文化将日期时间字符串从文本框发送到DateTime时, datetime 格式正在破坏 .

例如;我在查询字符串上发送这些参数 -

beginDate=02.01.2014&endDate=03.01.2014 ,这些参数值在传递ActionResult参数时显示如下 -

beginDate = 01.02.2014, endDate = 03.01.2014 .

我试了几个解决方案,但问题仍然存在 .

1)

protected void Application_Start()
{
    System.Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("tr-TR");
    System.Threading.Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("tr-TR");

}

2)

<globalization uiCulture="tr-TR" culture="tr-TR" />

3)

return Json(_db.GetList(Convert.ToDateTime(begin, new CultureInfo("tr-TR")), Convert.ToDateTime(end, new CultureInfo("tr-TR")))

4)

begin.Value.ToString("dd.mm.yyyy")
end.Value.ToString("dd.mm.yyyy")

Url

domain.com/Home/GetList?begin=02.01.2014&end=03.01.2014

GetList Method

public JsonResult GetList(DateTime? begin, DateTime? end)
{
    return Json(_db.GetList(Convert.ToDateTime(begin, new CultureInfo("tr-TR")), Convert.ToDateTime(end, new CultureInfo("tr-TR")))
}

1 回答

  • 0

    对于GET参数,默认模型 Binders 为 culture agnostic . 这意味着在将日期作为查询字符串参数发送到控制器操作时,应始终使用 yyyy-MM-dd 以下格式:

    ?beginDate=2014-01-02&endDate=2014-01-03
    

    如果您在POST请求的有效负载中发送这些日期,文化就会发挥作用 .

    这个实现细节背后有一个原因 . 您可以在以下文章中阅读更多相关信息:http://weblogs.asp.net/melvynharbour/archive/2008/11/21/mvc-modelbinder-and-localization.aspx

    这就是说你可以摆脱你试过的所有1.,2.,3.,4.,5.点,只需使用 beginend DateTime参数 . 您无需设置应用程序的区域性信息 .

相关问题