MVC 4 - DataAnnotations - Validation for Type

前端 未结 3 1993
傲寒
傲寒 2021-02-13 22:43

I have the following code working

    [Required(ErrorMessage = \"Price is required.\")]
    [Range(typeof(Decimal), \"1\", \"9999\", ErrorMessage = \"Price xx.xx         


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-13 23:10

    First off, I think you will want to change your Range attribute to

    [Range(typeof(Decimal), "1", "9999", ErrorMessage = "{0} must be a decimal/number between {1} and {2}.")]
    

    According to MSDN, this is the valid way to use RangeAttribute.

    Second:

    "The field productPrice must be a number."

    This is actually unobtrusive client-side JavaScript validation kicking in. Your range validator will fire after the number has been validated. You can disable the number validator although I do not recommend this:

    $.validator.methods.number = function (n, t) {
        return true;
    }
    

提交回复
热议问题