Created
August 31, 2023 13:40
-
-
Save vibs2006/d7c5554f43a5f81d062a2d4856b228bc to your computer and use it in GitHub Desktop.
Validation C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static List<string> GetListOfErrorMessagesFromModelState(Microsoft.AspNetCore.Mvc.ModelBinding.ModelStateDictionary modelState) | |
{ | |
if (modelState is null || modelState.Count == 0 || modelState.IsValid) return new List<string>(); | |
List<string> errors = new List<string>(); | |
foreach(var item in modelState.Values) | |
{ | |
foreach(var error in item.Errors) | |
{ | |
errors.Add(error.ErrorMessage); | |
} | |
} | |
return errors; | |
/* | |
//Short Form Notation | |
return ModelState.Values.SelectMany(m => m.Errors).Select(x => x.ErrorMessage).ToList() | |
*/ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment