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

  • Home
  • SEARCH
  • 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 894657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:25:44+00:00 2026-05-15T14:25:44+00:00

In ASP.NET MVC 2, I have a Linq to sql class that contains a

  • 0

In ASP.NET MVC 2, I have a Linq to sql class that contains a series of fields. Now I one of the fields is required when another field has a certain (enum) value.

I’ve come so far that I wrote a custom validation attribute, which can take an enum as an attribute, but I can’t say, for example: EnumValue = this.OtherField

How should I do that?

  • 1 1 Answer
  • 1 View
  • 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-05-15T14:25:45+00:00Added an answer on May 15, 2026 at 2:25 pm

    MVC2 comes with a sample “PropertiesMustMatchAttribute” that shows how to get DataAnnotations to work for you and it should work in both .NET 3.5 and .NET 4.0. That sample code looks like this:

    [AttributeUsage(AttributeTargets.Class, AllowMultiple = true, Inherited = true)] 
    public sealed class PropertiesMustMatchAttribute : ValidationAttribute 
    { 
        private const string _defaultErrorMessage = "'{0}' and '{1}' do not match."; 
    
        private readonly object _typeId = new object(); 
    
        public PropertiesMustMatchAttribute(string originalProperty, string confirmProperty) 
            : base(_defaultErrorMessage) 
        { 
            OriginalProperty = originalProperty; 
            ConfirmProperty = confirmProperty; 
        } 
    
        public string ConfirmProperty 
        { 
            get; 
            private set; 
        } 
    
        public string OriginalProperty 
        { 
            get; 
            private set; 
        } 
    
        public override object TypeId 
        { 
            get 
            { 
                return _typeId; 
            } 
        } 
    
        public override string FormatErrorMessage(string name) 
        { 
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, 
                OriginalProperty, ConfirmProperty); 
        } 
    
        public override bool IsValid(object value) 
        { 
            PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(value); 
            // ignore case for the following
            object originalValue = properties.Find(OriginalProperty, true).GetValue(value); 
            object confirmValue = properties.Find(ConfirmProperty, true).GetValue(value); 
            return Object.Equals(originalValue, confirmValue); 
        } 
    } 
    

    When you use that attribute, rather than put it on a property of your model class, you put it on the class itself:

    [PropertiesMustMatch("NewPassword", "ConfirmPassword", ErrorMessage = "The new password and confirmation password do not match.")] 
    public class ChangePasswordModel 
    { 
        public string NewPassword { get; set; } 
        public string ConfirmPassword { get; set; } 
    } 
    

    When “IsValid” gets called on your custom attribute, the whole model instance is passed to it so you can get the dependent property values that way. You could easily follow this pattern to create even a more general comparison attribute.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ASP.NET MVC application which is using Linq to SQL classes placed
In my asp.net-mvc website I have a field that usually has a string (from
Assume I have an ASP.NET MVC app that's not doing anything too fancy (no
If I have an ASP.NET MVC controller action that is called from a jQuery
I have an ASP.NET MVC (using the release candidate) application that works with a
I'm writing an ASP.NET MVC site where I'm using LINQ to SQL to access
I am using ASP.NET MVC 2, .NET 4.0. I have a controller that disables
Does ASP.NET MVC have anything like the RESTful out-of-the-box features like Ruby on Rails
ASP.NET MVC routes have names when mapped: routes.MapRoute( Debug, // Route name -- how
Have just started playing with ASP.NET MVC and have stumbled over the following situation.

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.