Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Why can't I do like this?

[Required(ErrorMessage = "*")]
[RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessage = Resources.RegistrationModel.UsernameError)]
public string Username { get; set; }

What is the error message telling me?

An attribute argument must be a constant expression , typeof expression or array creation expression of an attribute parameter type.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
251 views
Welcome To Ask or Share your Answers For Others

1 Answer

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).

Setting resource access to public


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...