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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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).
Method 2
Please see this link: http://code.msdn.microsoft.com/Getting-Started-WCF-RIA-1469cbe2/sourcecode?fileId=19242&pathId=774666288 (link broken, but left for attribution purposes)
public sealed partial class RegistrationData
{
[Key]
[Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ErrorResources))]
[Display(Order = 0, Name = "UserNameLabel", ResourceType = typeof(RegistrationDataResources))]
[RegularExpression("^[a-zA-Z0-9_]*$", ErrorMessageResourceName = "ValidationErrorInvalidUserName", ErrorMessageResourceType = typeof(ErrorResources))]
[StringLength(255, MinimumLength = 4, ErrorMessageResourceName = "ValidationErrorBadUserNameLength", ErrorMessageResourceType = typeof(ErrorResources))]
public string UserName { get; set; }
Method 3
Try FluentModelMetaDataProvider.
Managed to use resources for error messages in strongly typed fashion.
Looks like this:
using System.Web.Mvc.Extensibility;
namespace UI.Model
{
public class StoreInputMetadata : ModelMetadataConfigurationBase<StoreInput>
{
public StoreInputMetadata()
{
Configure(m => m.Id)
.Hide();
Configure(model => model.Name)
.Required(Resources.Whatever.StoreIsRequired)
.MaximumLength(64, Resources.Whatever.StoreNameLengthSomething);
}
}
}
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.
It’s already self explanatory. C# isn’t dynamic language like Ruby where You can write classes that inherits random base class at runtime. 🙂
Here’s what Skeet says about this.
Method 4
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.
Method 5
You should instead look at the ErrorMessageResourceName and ErrorMessageResourceType properties of this attribute. They do allow the error message to be pulled from a resource.
Method 6
We can now use nameof for strongly typed error messages:
[RegularExpression("^[a-zA-Z0-9_]*$",
ErrorMessageResourceType=typeof(Resources.RegistrationModel),
ErrorMessageResourceName=nameof(Resources.RegistrationModel.UsernameError)
)]
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0
