视图模型特定类型的属性在Form Post中允许null

时间:2014-04-08 07:24:57

标签: c# validation asp.net-mvc-4 model modelstate

我有这个型号:

public class RegisterModel
    {
        [Required(ErrorMessage="Username is required")]
        [StringLength(50)]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "Password is required")]
        [StringLength(15, 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(ErrorMessage = "Email address is required")]
        [DataType(DataType.EmailAddress), RegularExpression(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", ErrorMessage = "Please enter a valid email address.")]
        [StringLength(100)]
        public string Email { get; set; }

        public RegisterViewModel RegisterVm { get; set; }

        public Resume Resume { get; set; }

        public Availability Availability { get; set; }

        public PostRequirements PostReq { get; set; }



    }

在表格后期操作中我正在检查模型是否有效继续其他不是,我想要的是,如果 PostReq 在这种情况下为空 ModelState 有效性可以为真,因为在在某些情况下,它将为null并且ifs为null我不想检查它以进行验证。

目前,如果为null,则ModelState无效。

我该怎么做?

我知道很久,int等我们可以让它们可以为空,但是如何在自定义类类型上做?

0 个答案:

没有答案