I have this :
public class Customer
{
[DisplayName("Lastname"), StringLength(50)]
[Required(ErrorMessage="My Error Message")]
[NotEmpty()]
public override string LastName { get; set; }
[DisplayName("Firstname"), StringLength(50)]
[Required(ErrorMessage="My Error Message 2")]
[NotEmpty()]
public override string FirstName{ get; set; }
}
In the controller, I do this :
if (!TryValidateModel(myCustomer))
{
//HERE
....
}
Where "HERE" is, I'd like get all error messages.
Some sample cases :
Any idea ?
Thanks,
You could get a list of all errors with their respective field and message like this:
var errors = ModelState
.Where(x => x.Value.Errors.Count > 0)
.Select(x => new { x.Key, x.Value.Errors })
.ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With