public async Task <ActionResult> Create(ApartmentBooking model) { //if (ModelState.IsValid) { var errorsResult = ApartmentBookingValidationContext.Validate(model); if (errorsResult.Any()) { ModelState.Clear(); foreach (var error in errorsResult) { ModelState.AddModelError(error.Key, error.Value); } TempData["ValidationErrors"] = true; return(View(model)); } else { db.ApartmentBookings.Add(model); await db.SaveChangesAsync(); } return(RedirectToAction("Index")); } //return View(model); }
public void DateFromIsNotGreaterThanNow() { ApartmentBooking model = new ApartmentBooking() { DateFrom = DateTime.Now.AddDays(-1) }; var errorsResult = ApartmentBookingValidationContext.Validate(model); if (errorsResult.Any()) { foreach (var error in errorsResult) { //error.Key, error.Value } } else { //ok } Assert.True(errorsResult.First().Key == "DateFrom"); }