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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:43:57+00:00 2026-05-12T10:43:57+00:00

I have a complete validation on an obeject and am trying to figure out

  • 0

I have a complete validation on an obeject and am trying to figure out the best way to handle it.

Given the following class:

public class LetterResponse {
 public Guid Id {get;set;}
 public bool SendBlankCart {get;set;}
 public string ToName {get;set;}
 public string ToAddress {get;set;}
}

I want to use a dataannotation and xval in order to validate the class before I persist it, but I have complex validation that requires more than one property.

Pseudo:

if SendBlankCart {
 - no validation on ToName, ToAddress 
} else {
 ToName - required.
 ToAddress - required. 
}

I would like to annotate like this:

[LetterResponseValidator]
public class LetterResponse {
 public Guid Id {get;set;}
 public bool SendBlankCart {get;set;}
 public string ToName {get;set;}
 public string ToAddress {get;set;}
}

and have a validation rule like this:

public class LetterResponseValidator : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            if (value.GetType() == typeof(LetterResponse))
            {
                var letterResponse = (letterResponse) value;
                if (letterResponse.SendBlankCard)
                {
                    return true;
                } else
                {
                    if (string.IsNullOrEmpty(letterResponse.FromDisplayName) || string.IsNullOrEmpty(letterResponse.ToAddress1))
                    {
                        return false;
                    }
                    return true;
                }

            }
            return false;
        }
    }

I am expecting the parameter to be my instance of the LetterResponse class, but it never gets called on my validation runner?

Does anyone know a way to handle this?

Thanks,

Hal

  • 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-05-12T10:43:57+00:00Added an answer on May 12, 2026 at 10:43 am

    I don’t think this has to do with the fact that you have a class-level validator. To exclude any connection, try applying a dummy required validator to “ToName” and see if the validator is called or not.

    If it is being called, then let me know, if it’s not, then you should check if you have overriden your standard modelbinder with the DataAnnotationsModelBinder in your Global.asax.cs file:

    ModelBinders.Binders.DefaultBinder = new DataAnnotationsModelBinder();
    

    For more details on this and fully working demo project, read this blog article about client-side validation.

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

Sidebar

Related Questions

I have the following class (from a simple Spring tutorial) public class CarValidator implements
Have recently been given a project to complete which uses XML quite extensively.Am looking
I have a Windows CE device that we are deploying, but we have complete
I have a complete XML document in a string and would like a Document
We have an auto-complete list that's populated when an you send an email to
I have to rename a complete folder tree recursively so that no uppercase letter
I have a byte array that represents a complete TCP/IP packet. For clarification, the
I have a series of forms that I need a user to complete in
I get a .pdf complete with images, fancy fonts, styles, gradients and what have
I'm starting a personal project, so I have at the moment complete architectural/design control.

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.