0

我创建了一个基于 Windows 窗体的应用程序,并且正在使用简单的会员帐户模型。在注册表单上,当应用程序上传到 azure 时,我遇到了日期格式问题。

在本地,我可以输入“31/10/2013”​​格式,但在 azure 托管应用程序上,我必须以“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; }
}

我尝试将日期格式应用于上述 DOB 属性,但在天蓝色上它仍在验证 MM/DD/YYYY

4

1 回答 1

3

The FormatAttribute of the Mvc Controls Toolkit enables the developer to define the format to be used in the representation of a property on the client side, as shown in the example below :-

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

This way, the strings can be globalized as explained here.

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
于 2013-10-16T10:11:53.933 回答