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??
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:
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:
And your view model classes become simply:
And your controller actions stay untouched except that they now behave as expected.