0

i need to simply store the generic validation messages for Compatibility i have tried something like this

public static class GemericMessages
    {
        public static readonly string Required = "Required Filed";
    }

[Required(ErrorMessage = GemericMessages.Required)]
        public string UserName { get; set; }

but i'am facing error say's : "An attribute argument must be a constant expression, typeof expression or array creation expression"

what are the best practice to done this , and how to fix this error please ?

2 Answers 2

1

Vendettamit's answer is correct, but if you don't wish to use resources for some reason, it will work to mark your string const instead of readonly.

public const string Required = "Required Filed";
Sign up to request clarification or add additional context in comments.

Comments

1

You an use resource string instead. An overload of Required field attribute allows you to pass in resource class and respective fieldName.

[Required(ErrorMessageResourceType = typeof(Resource1), ErrorMessageResourceName = "requiredField")]

To add a resource file if it's not already exists in your project simply right click on your project. Add new item and select resource file. After adding it in the project you can double click to open it and enter your string for validation message:

enter image description here

2 Comments

thanks for replay but is this the best way to done this or i need to customize or override the attribute it self ? what do you think ?
This is the quickest and easiest way to achieve it using out of the box api.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.