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 8797683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:54:09+00:00 2026-06-13T23:54:09+00:00

I am attempting to create a custom validation attribute for my MVC application. My

  • 0

I am attempting to create a custom validation attribute for my MVC application. My code as written, works great for the properties specified in the code. I now want to expand it, so it is more general, because I have 5 other properties I would like to use this same attribute on.
The general idea is if the specified other property is true, then the property attached to the attribute must be > 0.

I assume the way to do this is to create a constructor that accepts the value of the property and the value of the other property, but I can’t seem to get it going. The specific problem I having is I can’t find the correct way to pull in the needed values.

Here is what I have:

public class MustBeGreaterIfTrueAttribute : ValidationAttribute
{
    protected override ValidationResult IsValid(object value, ValidationContext context)
    {
        var model = context.ObjectInstance as HistoryViewModel;
        //ValidationResult result = null;

        // Validate to ensure the model is the correct one
        if (context.ObjectInstance.GetType().Name == null)
        {
            throw new InvalidOperationException(string.Format(
                    CultureInfo.InvariantCulture, "Context of type {0} is not supported. "
                                                  + "Expected type HistoryViewModel",
                                                   context.ObjectInstance.GetType().Name));
        }

        // Here is the actual custom rule
        if (model.HistoryModel.IsRetired == true)
        {
            if (model.CounterA == 0)
            {
                return new ValidationResult("Please enter more information regarding your History");
            }
        }
        else if ( model.HistoryModel.IsRetired == true )
        {
            if ( model.ROCounter > 0 )

            return ValidationResult.Success;
        }

        // If all is ok, return successful.
        return ValidationResult.Success;

    }

    //  Add the client side unobtrusive 'data-val' attributes
    //public IEnumerable<ModelClientValidationRule> GetClientValidationRules(ModelMetadata metadata, ControllerContext context)
    //{

    //}

}

Thank you for you time.

  • 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-13T23:54:10+00:00Added an answer on June 13, 2026 at 11:54 pm

    I just did something similar in an attribute I called “RequiredIfTrueAttribute”. This will get you the value of the other property in the model. Pass the other property name as a string into the custom attribute constructor.

    public class RequiredIfTrueAttribute: ValidationAttributeBase
    {
    
        public string DependentPropertyName { get; private set; }
    
        public RequiredIfTrueAttribute(string dependentPropertyName) 
            : base()
        {
                this.DependentPropertyName = dependentPropertyName;
        }
    
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            // Get the property we need to see if we need to perform validation
            PropertyInfo property = validationContext.ObjectType.GetProperty(this.DependentPropertyName);
            object propertyValue = property.GetValue(validationContext.ObjectInstance, null);
    
            // ... logic specific to my attribute
    
            return ValidationResult.Success;
        }
    
    
    }
    

    Now if only there was a way to pass the dependentPropertyName to the validation attribute without using a string…


    Update:

    In C# 6.0 there is now a way to call the dependentPropertyName without using a string. Simply use nameof(thePropertyName) and it will be replaced with the string. This happens in compile time so if you change the property name you will immediately know that you need to change this as well. Or, even better, if you do a Ctrl+R, Ctrl+R to rename the variable it will automatically rename the version inside the nameof as well. Awesome!

    See: nameof (C# and Visual Basic Reference)

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

Sidebar

Related Questions

I am attempting to create a custom attribute that can be assigned to an
I'm attempting to create themeing system for my asp.net mvc application. I was able
In my ASP.NET MVC 2 web application, I allow users to create custom input
I'm attempting to create a custom validation for one of my models in Rails
I'm attempting to create a custom named attribute in my project template at the
I attempting to create custom tabs using this . But when I try to
I am attempting to create custom colors of the tabs in a TabHost. I
I am attempting to create a custom Adapter for my ListView since each item
I am attempting to create my own custom Autocomplete style dropdown control in c#
So, horray - I'm attempting to create a new custom Payment Gateway. It's designed

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.