Error messages for model validation using data annotations
- by oneBelizean
Given the follow classes:
using System.ComponentModel.DataAnnotations;
public class Book{
public Contact PrimaryContact{get; set;}
public Contact SecondaryContact{get; set;}
[Required(ErrorMessage="Book name is required")]
public string Name{get; set;}
}
public class Contact{
[Required(ErrorMessage="Name is required")]
public string Name{get; set;}
}
Is there a clean way I can give a distinct error message for each instance of Contact in Book using DataAnnotations? For example, if the name was missing from the PrimaryContact instance the error would read "primary contact name is required".
My current solution is to create a validation service that checks the model state for field errors then remove said errors and add them back using the specific language I'd like.