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 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 that uses Linq to SQL repositories for all
Ok the thing is that I am using asp.net mvc with linq to sql.
I have a ASP.NET MVC application and I have used Linq-to-SQL queries to get
I created one asp.net mvc application using linq to sql, and in the generated
I have this web app that is running ASP .NET MVC 1.0 with LINQ
I have an ASP.NET MVC application which is using Linq to SQL classes placed
I have an application using ASP.NET MVC, Unity, and Linq to SQL. The unity
I have an ASP.NET MVC 3 Web Application using Linq-to-SQL for my data access
I'm studying ASP.NET MVC and I use LINQ to SQL for model. I have
I'm trying to do a Query with LINQ on ASP.NET MVC 3. I have

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.