using Common.Models.ModelValidation; using Common.Models.RequestResponse; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; namespace Common.Utility { public class CustomModelValidaton { public TPResponse IsModelValid(T model) where T : class { var jm = new ValidationContext(model, serviceProvider: null, items: null); var results = new List(); bool IsValid = Validator.TryValidateObject(model, jm, results); if (!IsValid) { var errorList = results.Select(x => new FieldsValidation() { Name = x.MemberNames.First(), Message = x.ErrorMessage }).ToList(); TPResponse tPResponse = new TPResponse() { ResponseCode = "104", Msg = "Model Validation Failed !", Data = errorList }; return tPResponse; } return null; } } }