Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7850513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:52:51+00:00 2026-06-02T18:52:51+00:00

At the moment I am writing my own ValidationAttribute for my mvc application. I

  • 0

At the moment I am writing my own ValidationAttribute for my mvc application.

I have following ValidationAttribute code.

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class | AttributeTargets.Parameter, AllowMultiple = false)] 
public class RecordAttribute: ValidationAttribute
{

   public UniqueDataRecordAttribute(string primaryKeyProperty)
   {

   }
}

I pass the field name of my primary property as a string to my attribute and make sone validation.
E.g.:

[RecordAttribute("CustomerID")]
public class CustomerMetaData
{


}

This works for me, but I will run into problems if the name of the primary key will change.

I created a enum which contains the primary key attribute. But when I try to pass it the compiler is telling me:

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

I also tried this approach: Associating enums with strings in C# but the effect is the same.

Is there any chance to pass a enum (or other compiled value) to my attribute?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:52:53+00:00Added an answer on June 2, 2026 at 6:52 pm

    Are you trying to do something like this?

    [RecordAttribute(Keys.CustomerID.ToString())] 
    public class CustomerMetaData 
    { 
    }
    

    That won’t work because the string returned by Keys.CustomerID.ToString() isn’t a constant.

    Instead of an enum could you use a static class of const string fields?

    static class Keys {
      public const string CustomerID = "CustomerID";
    }
    

    Then this will work:

    [RecordAttribute(Keys.CustomerID)] 
    public class CustomerMetaData 
    { 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

At the moment, I have a goal of writing an application that contains a
I am developing a simple WinAPI application and started from writing my own assertion
I am integrating third party code into the web application I am writing in
I'm developing an Android game. At the moment I'm writing code that should calculate
I am playing with .NET MVC at the moment and I writing some unit
I'm having a go at writing my own toy OS and for the moment
I am writing a complex AJAX application at the moment and the entire site
I am writing an application that will have some users on Internet Explorer 6.
I'm writing some C at the moment and because I like whitespace sensitive syntax,
I am writing a Ruby app at the moment which is going to search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.