In my model I have a person with a HomeAddress and BillingAddress of type location:

class Person {
public Location HomeAddress { get; set; }
public Location BillingAddress { get; set; }
... other properties
}

class Location {
[Required]
public string ZipCode { get; set; }
... other properties
}

I'm looking for an elegant way to validate the 'required' properties only on the required property HomeAddress. I'm using the normal componentmodel attributes and built in model validation from MVC. So what I'm looking for is an attribute that would check if the Location is in Person.HomeAddress or .BillingAddress, and only validate in the first case. Or should I change my design and make the bindingaddress NULL in case not enough info is entered?

有帮助吗?

解决方案

One option is you could implement IValidatableObject in your Person class. This would define a Validate method which MVC will call automatically. Then you can programmatically add logic around what Location entries are required, and validate the fields there.....

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top