0

I have an API Controller with the following Action header:

public IHttpActionResult Post(InvoicesDTO invoices)

where InvoicesDTO is as follows:

public class InvoicesDTO
    {
        public int IdHeader { get; set; }
        [DataType(DataType.DateTime)]
        public DateTime DateSend { get; set; }
        public List<InvoiceDTO> ListInvoices { get; set; }
    }

and InvoiceDTO is a POCO class with some DataAnnotations validations.

The problem is that i want, when a validation error occurs, to know which InvoiceDTO of the InvoicesDTO's list is the one that has generated the error to be able to do something like this:

if (ModelState.IsValid)
{
}
else
{    
_logger.LogError($"Validation Error at Invoice -
{ModelState.Values.First().WHATEVER_TO_GET_THE_ELEMENT_THAT_GENERATED_THE_ERROR");
}

thanks!

MorgoZ
  • 2,012
  • 5
  • 27
  • 54

1 Answers1

0

You can iterate through all the properties of the Model and check the validity for individual property.

bool isvalid = ModelState.IsValidField("Model Property");

Roshan
  • 31
  • 3