1

I have a (Settings) class that contains all hardcoded code. It's really handy for certain fields such as maxCharactersFields and error messages, this way I can use the same field for mapping, models & viewmodels. Therefore if it were to change it the future, everything changes the same way. However, I cannot seem to use this in viewmodels. More specifically in StringLength of System.ComponentModel.DataAnnotations.

The error it gives is "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type."

Certain things that I've already tried is replacing it with a field in the ViewModel in which I'm working, but it gives the same error. I've googled and searched on StackOverflow but can't seem to find anyone who tried to do something like this and ran into the same problem.

What I've learned so far is that I can't use my Settings class because it's not a basic type but is there a way around it?

The error occurs in the line of StringLength.

[Display (Name = "E-mail van de gebruiker", Prompt = "John.Doe@gmail.com")]
        [DataType (DataType.EmailAddress)]
        [Required]
        [StringLength(Settings.maxCharactersEmail)]
        public string Email { get; set; }
    public static class Settings
    {
....
        public static readonly int maxCharactersEmail= 320; //Googled it
....
    }

Ruben Szekér
  • 1,065
  • 1
  • 10
  • 21
  • If you want the validation rules to be configurable you'll have to validate models in code. As for "hard-coded settings" there's no reason for them. You can read configuration from various sources. You can use the same configuration system used in .NET Core in the Full Framework too just by adding the Microsoft.Extensions.Configuration package to your application, and read configuration settings from multiple file formats, databases, environment variables etc. The same holds for Dependency Injection. – Panagiotis Kanavos Apr 18 '19 at 08:40

1 Answers1

1

It doesn't actually have anything to do your settings class type. Attributes are a compile-time thing, so you can't use static or instance values. You have to use constant values (public const int):

public static class Settings
{
    public const int maxCharactersEmail= 320; //Googled it
}

Your attribute will now work:

[Display (Name = "E-mail van de gebruiker", Prompt = "John.Doe@gmail.com")]
[DataType (DataType.EmailAddress)]
[Required]
[StringLength(Settings.maxCharactersEmail)]
public string Email { get; set; }
ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
  • However, is there a way I could use the int from my Settings class somehow by casting or finding a loophole because if it were to change then I have to change it in 2 locations everytime. For one attribute this doesn't matter, but there are about 200 and it would be very easy to forget one. – Ruben Szekér Apr 18 '19 at 08:38
  • Oh I read over it, thanks a lot! You saved me big time! Have a great day! – Ruben Szekér Apr 18 '19 at 08:42