public IActionResult Create() { var technician = _appointmentService.GetTechnician(User.Identity.Name); if (technician == null) { return(NotFound()); } var model = new AppointmentCreateModelView() { TechnicianName = technician.Name, TechnicianId = technician.TechnicianId, TechnicianEmail = technician.Email }; return(View(model)); }
[ValidateAntiForgeryToken] //Prevents cross-site Request Forgery Attacks public async Task <IActionResult> Create(AppointmentCreateModelView model) { if (model.StartDate >= model.ExpectedFinalDate) { ModelState.AddModelError("StartDate", "A data de início deve ser anterior à data de término"); return(View()); } if (model.StartDate < DateTime.Today) { ModelState.AddModelError("StartDate", "A data de início deve ser posterior à data atual"); return(View()); } if (ModelState.IsValid) { var appointment = new Appointment { Id = model.AppointmentId, TechnicianId = model.TechnicianId, TechnicianName = model.TechnicianName, Observations = model.Observations, StartDate = model.StartDate, ExpectedFinalDate = model.ExpectedFinalDate, Address = model.Address, AppointmentName = model.AppointmentName, City = model.City, District = model.District, Number = model.Number, Postcode = model.Postcode, State = model.State }; await _appointmentService.CreateAsync(appointment); return(RedirectToAction(nameof(Index))); } return(View()); }