public IEnumerable <ValidationResult> Validate(ValidationContext validationContext) { var validator = new CourseViewModelValidator(); var result = validator.Validate(this); return(result.Errors.Select(item => new ValidationResult(item.ErrorMessage, new[] { item.PropertyName }))); }
public JsonResult Add(ClassesViewModel courseToAdd) { try { var validationResult = _courseViewModelValidator.Validate(courseToAdd); if (!validationResult.IsValid) { return(Json(new { result = validationResult.Errors.Select(x => x.ErrorMessage).ToList() }, JsonRequestBehavior.AllowGet)); } var ClassToAdd = Mapper.Map <Classes>(courseToAdd); ClassesRepository.Add(ClassToAdd); return(Json(new { result = "success" }, JsonRequestBehavior.AllowGet)); } catch (Exception e) { return(Json(new { result = e.Message }, JsonRequestBehavior.AllowGet)); } }