.NET 4 MVC 2 Validation with annotations warning instead of error

后端 未结 5 1435
轮回少年
轮回少年 2020-12-30 07:53

I am using .NET 4 with MVC 2 for validating with Annotations. Is there a (simple) solution for giving back a warning instead of the error?

5条回答
  •  太阳男子
    2020-12-30 08:33

    All validation Attributes have ErrorMessage property. You can give your custom error message

    public class Product
    {
    
        [StringLength(50)]
        [Required(ErrorMessage = "Your user friendly message")]
        public string Name { get; set; }
    
    }
    

    Then change the CSS styles of the error messages.

    /* Styles for validation helpers
    -----------------------------------------------------------*/
    .field-validation-error
    {
        color: #ff0000;/*change the color*/
    }
    
    /*Other validation related styles*/
    

提交回复
热议问题