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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T03:16:28+00:00 2026-05-17T03:16:28+00:00

I have some problems with validation using Data Annotations in ASP.NET MVC 2. For

  • 0

I have some problems with validation using Data Annotations in ASP.NET MVC 2. For example, I have Address class:

public class Address
{
    public long Id { get; set; }

    [Required]
    public string City { get; set; }

    [Required]
    public string PostalCode { get; set; }

    [Required]
    public string Street { get; set; }
}

And Order class:

public class Order
{
    public long Id { get; set; }

    public Address FirstAddress { get; set; }

    public Address SecondAddress { get; set; }

    public bool RequireSecondAddress { get; set; }
}

I want to validate Order.FirstAddress all the time, but Order.SecondAddress should be validated only if Order.RequireSecondAddress is set to true.

Any ideas? 🙂

Chris

  • 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-17T03:16:29+00:00Added an answer on May 17, 2026 at 3:16 am

    That’s close to impossible using data annotations or it will require writing ugly code that relies on reflection, etc… (I think you get the point).

    I would recommend you looking at the FluentValidation. It has a good integration with ASP.NET MVC. Here’s how your validation logic might look like:

    public class AddressValidator : AbstractValidator<Address>
    {
        public AddressValidator()
        {
            RuleFor(x => x.City)
                .NotEmpty();
            RuleFor(x => x.PostalCode)
                .NotEmpty();
            RuleFor(x => x.Street)
                .NotEmpty();
        }
    }
    
    public class OrderValidator : AbstractValidator<Order>
    {
        public OrderValidator()
        {
            RuleFor(x => x.FirstAddress)
                .SetValidator(new AddressValidator());
            RuleFor(x => x.SecondAddress)
                .SetValidator(new AddressValidator())
                .When(x => x.RequireSecondAddress);
        }
    }
    

    You will also benefit from having a separate validation layer which also could be unit tested in a very elegant way.

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

Sidebar

Related Questions

I am developing an ASP.NET MVC app and I've been looking into using Data
I'm using ASP.NET MVC 3 and DataAnnotations validations and for some reason the javascript
I have some problems using ajax with Spring MVC. I want to refresh only
I have an asp.net mvc application and I'm using Webgrid to list customers. I
How do I remain DRY with asp.net mvc view models & data annotation (validation,
We are trying to create some custom validation with MVC 4 data annotations, the
I’m having some problems with validation. I have created a custom validator called RequiredFieldRule
I have some problems sending mails through SMTP using Spring's MailSender interface and the
I have some problems with OpenCV s cvCanny(...) and the Image data types it
I'm using the jQuery Validation plugin on my forms. I have some groups of

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.