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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:43:17+00:00 2026-05-18T06:43:17+00:00

For example i have public class Person { public int Id {get;set;} [Required()] public

  • 0

For example i have

public class Person
{
    public int Id {get;set;}
    [Required()]
    public string Name {get;set;}
    [Required()]
    public Address Address {get;set;}
}

And

public class Address
{
    public int Id {get;set;}
    [Required()]
    public string City {get;set;}
    [Required()]
    public string Street {get;set;}
}

I need to validate every property in Address, but when validating Person i need to validate only the id of Address.
How to do that??

  • 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-18T06:43:18+00:00Added an answer on May 18, 2026 at 6:43 am

    I don’t know what do you mean by I need to validate every property in Address, but when validating Person i need to validate only the id of Address. Correct me if I am wrong but here’s how I understand your question: you have two different controller actions:

    [HttpPost]
    public ActionResult ValidateAddress(Address address)
    {
        ... // validate all properties of address
    }
    
    [HttpPost]
    public ActionResult ValidatePerson(Person person)
    {
        ... // validate only the Id of a person's Address
    }
    

    Well personally I would use FluentValidation instead of Data Annotations as it allows you to express your validation logic in a much cleaner way and among others handle cases like this one. So here’s how this could be expressed in an elegant way:

    /// <summary>
    /// Validates all properties of an address
    /// </summary>
    public class AddressValidator : AbstractValidator<Address>
    {
        public AddressValidator()
        {
            RuleFor(x => x.Id).NotEmpty();
            RuleFor(x => x.City).NotEmpty();
            RuleFor(x => x.Street).NotEmpty();
        }
    }
    
    /// <summary>
    /// Validates only the id of an address
    /// </summary>
    public class PersonAddressValidator : AbstractValidator<Address>
    {
        public PersonAddressValidator()
        {
            RuleFor(x => x.Id).NotEmpty();
        }
    }
    
    /// <summary>
    /// Validates a Person
    /// </summary>
    public class PersonValidator : AbstractValidator<Person>
    {
        public PersonValidator()
        {
            RuleFor(x => x.Name).NotEmpty();
            RuleFor(x => x.Address).SetValidator(new PersonAddressValidator());
        }
    }
    

    And your view model classes become simply:

    [Validator(typeof(PersonValidator))]
    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Address Address { get; set; }
    }
    
    [Validator(typeof(AddressValidator))]
    public class Address
    {
        public int Id { get; set; }
        public string City { get; set; }
        public string Street { get; set; }
    }
    

    And your controller actions stay untouched except that they now behave as expected.

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

Sidebar

Related Questions

For example I have a simple class like public class Person{ public int Age
For example, suppose I have a class: class Foo { public: std::string& Name() {
I have a simple class as defined below: public class Person { int _id;
I have a custom c# type like (just an example): public class MyVector {
If you have, for example, a database table called Person (ID,Name etc) what kind
For example if we have a class Person and a class Student extends Person.
I have a Enum for example... public enum TypeIdentifier { NotSet = 0, Type1=
I have an interface - here's a nicely contrived version as an example: public
Example I have Person , SpecialPerson , and User . Person and SpecialPerson are
Guys I have a best practice question For example I have this classes: class

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.