Alex Rivera | Logout

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

Asked 2010-04-22T07:17:58.287
21

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.

Edit
Report

1 Answer

0

It means that you cannot determine the value of the argument you are passing into the attribute at runtime, it must be at compile time so the value is embedded into the assembly.

The error message value needs to be a constant expression.

For information, attribute arguments can only be of types bool, byte, char, short, int, long, float, double, string, System.Type, and enums.

answered 2010-04-22T07:27:07.867

Your Answer