Model Binder - 如何制作可选项

时间:2013-03-15 12:05:37

标签: 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; }
}

我尝试将所有属性一起删除,但它仍然按需要对其进行分类。

我哪里错了?

1 个答案:

答案 0 :(得分:6)

你应该让它可以为空

public DateTime? DateOfBirth { get; set; }
相关问题