将日期格式解析为特定文化

sto*_*oic 3 c# asp.net-mvc-2

我住在南非文化en-ZA,我们的日期格式是以格式输入的 dd/mm/yyyy

我有一个接受模型的视图:

public class UserInfoModel
{
    public DateTime DateOfBirth{get;set;}
    // some other properties here
}
Run Code Online (Sandbox Code Playgroud)

当用户输入日期即:04/15/1981我在post方法中获得的日期时间是1981年4月15日,但是,当插入以下日期时,15/04/1981返回的模型中的DateOfBirth属性是null

有没有办法可以改变全局解析日期的方式(在我的应用程序中)

我在web.config中添加了以下内容:

<system.web>
  <globalization culture="en-ZA" uiCulture="en-ZA"/>
</system.web>
Run Code Online (Sandbox Code Playgroud)

但它似乎没有什么区别.

kle*_*had 6

尝试将此添加到您的GlobalAsax.cs(位于您的App_code目录中)

protected void Application_BeginRequest(object sender, EventArgs e)
{
    CultureInfo cInfo = new CultureInfo("en-ZA");
    cInfo.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy";
    cInfo.DateTimeFormat.DateSeparator = "/";
    Thread.CurrentThread.CurrentCulture = cInfo;
    Thread.CurrentThread.CurrentUICulture = cInfo;
}
Run Code Online (Sandbox Code Playgroud)