using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using Microsoft.AspNetCore.Mvc.ModelBinding; namespace Infrastructure.Extensions { public static class ModelStateDictionaryExtensions { public static void AddModelError(this ModelStateDictionary modelState, IList validationResults) { foreach (var item in validationResults) { if (item.MemberNames.Any()) { foreach (var key in item.MemberNames) { modelState.AddModelError(key, item.ErrorMessage); } } else { modelState.AddModelError("", item.ErrorMessage); } } } } }