Search code examples
asp.net-mvc-3unobtrusive-validationrequired-field

MVC3 Required validation, choose value for empty


I have a hidden field which is bound to a int Id in the model, it has a required attribute and some fancy ajax code to set the id client side, the problem is that zero should be acounted as empty. Now the validation will succeed even if no Id has been selected, bow can I set which value should be counted as empty? I hope i do not need to create a custom validator for it.

Thanks


Solution

  • It doesn't maker sense to add required attribute to a non nullable type such as Int32. Value types are always required. You could use a nullable integer instead:

    [Required]
    public int? SomeProperty { get; set; }