从视图到模型的日期时间格式

DateTime Format from View to Model

我正在为讲法语的人开发一个应用程序,其日期的正常书写方式类似于 dd-MM-yyyy 或 yyyy-MM-dd。有没有办法告诉模型绑定另一种日期格式?现在,系统会切换月份和日期,或者如果日期(成为月份)>12,则 returns 为空。 谢谢

当从 ui 提交详细信息时,尝试以 required 格式提交。 (格式化所有日历插件中可用的日期时间)

并创建这样的自定义区域性以强制后端 (asp.net ) 始终使用 required 格式。

将此代码包含在 global.asax.cs

 protected void Application_BeginRequest(Object sender, EventArgs e) // Customizing / Intializing DateTime Format to yyyy/MM/dd to avoid conflicts 
    {
        CultureInfo newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
        newCulture.DateTimeFormat.ShortDatePattern = "yyyy/MM/dd";
        newCulture.DateTimeFormat.DateSeparator = "/";
        Thread.CurrentThread.CurrentCulture = newCulture;
    }

如果您希望可以有多种格式之一的日期,则将其作为字符串绑定到模型,在控制器端定义其格式,然后转换为 DateTime。