Model Binder - 如何制作可选项

JGi*_*tin 1 c# asp.net-mvc-4 .net-4.5

我希望使属性DateOfBirth与模型绑定器相关

public class RegisterModel
{
    [Required]
    public int MP_CustomerID { get; set; }

    [Required]
    [DataType(DataType.Password)]     
    public string Password { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Compare("Password")]
    public string PasswordConfirm { get; set; }

    [Required(AllowEmptyStrings = true, ErrorMessage="This is the error message")]
    public DateTime DateOfBirth { get; set; }

    public int NoOfVehicles { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试一起删除属性,但它仍然按需要分类.

我哪里错了?

dev*_*tal 6

你应该让它可以为空

public DateTime? DateOfBirth { get; set; }
Run Code Online (Sandbox Code Playgroud)