覆盖MVC4应用程序中AccountModel的默认日期格式验证

时间:2013-10-16 10:05:43

标签: asp.net-mvc-4 datetime azure simplemembership

我创建了一个基于Windows窗体的应用程序,并使用简单的成员身份AccountModel。在注册表单上,当应用程序上传到azure时,我遇到日期格式问题。

本地我可以输入“31/10/2013”​​格式,但在天蓝色托管应用程序中,我必须以“10/31/2013”​​格式输入。如何覆盖验证,以便允许使用DD / MM / YYYY格式

我的注册模型如下:

public class RegisterModel
{
    [Required]
    [Display(Name = "Username")]
    public string UserName { get; set; }

    [Required]
    [Display(Name = "Forename")]
    public string Forename { get; set; }

    [Required]
    [Display(Name = "Surname")]
    public string Surname { get; set; }

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

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Secret Answer")]
    public string SecretAnswer { get; set; }
}

我尝试将dateformat应用于DOB属性,但是在azure上它仍在验证MM / DD / YYYY

1 个答案:

答案 0 :(得分:3)

Mvc Controls Toolkit的FormatAttribute使开发人员能够定义在客户端的属性表示中使用的格式,如下例所示: -

    Format(Prefix="Date of birth is: ", ClientFormat = "d", NullDisplayText="No date of birth available")]
    public DateTime? BirthDate { get; set; }

这样,字符串可以全球化,如此处所述。

Below the list of all supported date format:
Format  Meaning     "en-US"
f   Long Date, Short Time   dddd, MMMM dd, yyyy h:mm tt
F   Long Date, Long Time    dddd, MMMM dd, yyyy h:mm:ss tt
G   Short Date, Long Time   M/d/yyyy h:mm:ss tt
t   Short Time  h:mm tt
T   Long Time   h:mm:ss tt
d   Short Date  M/d/yyyy
D   Long Date   dddd, MMMM dd, yyyy
Y   Month/Year  MMMM, yyyy
M   Month/Day   yyyy MMMM