【问题标题】:How is the format for server-side date validation configured in ASP.NET MVC 4?ASP.NET MVC 4 中如何配置服务器端日期验证的格式?
【发布时间】:2012-12-12 22:35:27
【问题描述】:

ASP.NET MVC 4 中服务器端日期验证的格式是如何配置的?例如,以下模型字段('BirthDate')仅根据客户端上的格式 'MM/dd/yyyy' 进行验证。在我的服务器上,当 POST 数据绑定到模型时,显然需要格式“dd/MM/yyyy”。

[Required]
[Display(Name = "Date of Birth")]
[DisplayFormat(DataFormatString="{0:MM/dd/yyyy}", ApplyFormatInEditMode=true)]
[DataType(DataType.Date)]
public DateTime BirthDate { get; set; }

【问题讨论】:

    标签: asp.net-mvc date asp.net-mvc-4 datepicker validation


    【解决方案1】:

    ASP.NET MVC 服务器上的日期验证与配置的区域性相关联。默认情况下,文化是未指定的,而是采用 Web 客户端的文化。因此,例如要在服务器上使用美国日期约定,编辑您的 Web.config 的 configuration/system.web/globalization 元素如下(uiCulture 不应该影响此验证,但我已将其添加为很好的措施):

    <globalization culture="en-us" uiCulture="en-us"></globalization>
    

    【讨论】:

      猜你喜欢
      • 2013-01-28
      • 2013-10-09
      • 2017-05-21
      • 2014-09-29
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多