I have a model on which I'm using validation by data annotations, like this :
public int Id { get; set; }
[Required(ErrorMessage = "Please enter the Sample Description.")]
public string Description { get; set; }
[Required(ErrorMessage = "Please enter the Start Date.")]
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
Data gets inserted via an API and, if validation fails, I would like to take any returned error messages and put them directly out to the screen.
This is fine for something like this :
[Required(ErrorMessage = "Please enter the Sample Description.")]
public string Description { get; set; }
But in the case of the integer property shown, "Id", and the client sending, a value that can't be cast to an integer, for instance "x", then you get returned a low level message like this :
"Could not convert string to integer: x. Path 'Id', line 1, position 9.
What I really want is a message more like
"Please provide an integer for the Id".
Is there anyway to allow data validation by annotation and still provide a more useful message than what I'm getting ?