public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var validator = new CompanyViewModelValidator(); var result = validator.Validate(this); return(result.Errors.Select(item => new ValidationResult(item.ErrorMessage, new[] { item.PropertyName }))); }
public async Task <IActionResult> OnPostAsync() { if (!PageContext.ModelState.IsValid) { var validator = new CompanyViewModelValidator(); var createCheck = validator.Validate(Company); createCheck.AddToModelState(ModelState, nameof(Company)); return(Page()); } var company = new Company { Name = Company.Name, Nip = Company.Nip, BankAccountNumber = Company.BankAccountNumber, OwnerId = SelectedUser }; await _companyService.AddAsync(company); return(Redirect("/")); }