Why can't I use resources as ErrorMessage with DataAnnotations?

前端 未结 6 1985
眼角桃花
眼角桃花 2020-12-05 05:15

Why can\'t I do like this?

[Required(ErrorMessage = \"*\")]
[RegularExpression(\"^[a-zA-Z0-9_]*$\", ErrorMessage = Resources.RegistrationModel.UsernameError)         


        
6条回答
  •  醉话见心
    2020-12-05 05:49

    When you are using the ErrorMessage property only constant strings or string literal can be assigned to it.

    Use the ErrorMessageResourceType and ErrorMessageResourceName instead to specity your resources.

    [RegularExpression(
        "^[a-zA-Z0-9_]*$", 
        ErrorMessageResourceType=typeof(Resources.RegistrationModel),
        ErrorMessageResourceName= "UsernameError"
    )]
    

    Note that the resources must be public (can be set in the resource editor).

提交回复
热议问题