If I have a class like this:
class Sausage
{
[Required]
public string Location {get;set;}
[Required]
public decimal Lat {get;set;}
[Required]
public decimal Lng {get;set;}
}
If Sausage.Lat
or Sausage.Lng
is not filled out, I would like to populate an error message onto Sausage.Location
instead... Is there an easy way of doing this?
The code might look like this:
class Sausage
{
[Required]
public string Location {get;set;}
[Required]
[ErrorFor(Location, "The Location must be filled out")]
public decimal Lat {get;set;}
[Required]
[ErrorFor(Location, "The Location must be filled out")]
public decimal Lng {get;set;}
}
I've implemented the IValidatableObject interface previously to help with this.
There are other ways like creating custom validation attributes but I find this more easy for me: