public ActionResult Add(WorkshopEditModel model) { if (!ModelState.IsValid) { // Pass the action to the form so it knows to return to Add ViewData["Action"] = "Add"; // Add list of projects and employees to choose from LoadListsForEdit(model); return(View(model)); } // Provider code must be unique. Validate if (!_workshopService.SessionIdentifierIsUnique(model.SessionIdentifier)) { ModelState.AddModelError("", "Session Identifier already used. " + " Please enter a unique session identifier."); // Pass the action to the form so it knows to return to Add ViewData["Action"] = "Add"; // Add list of projects and employees to choose from LoadListsForEdit(model); return(View(model)); } // Add workshop to db model = _workshopService.AddOrUpdate(model); return(RedirectToAction(nameof(Details), new { id = model.Id })); }
/// <summary> /// Gets workshop edit view model. /// </summary> /// <param name="id"></param> /// <returns></returns> public WorkshopEditModel GetForEdit(int id) { var workshopEditModel = new WorkshopEditModel(); var workshop = _workshopRepo.Get(id); if (workshop != null) { workshopEditModel = _mapper.Map <WorkshopEditModel>(workshop); } return(workshopEditModel); }
/// <summary> /// Creates new workshop if Id is zero; otherwise updates workshop. /// </summary> /// <param name="workshopEditModel"></param> public WorkshopEditModel AddOrUpdate(WorkshopEditModel workshopEditModel) { var workshop = _mapper.Map <Workshop>(workshopEditModel); if (workshopEditModel.Id == 0) { _workshopRepo.Add(workshop); // Get Id from new workshop workshopEditModel.Id = workshop.Id; } else { _workshopRepo.Update(workshop); } return(workshopEditModel); }
// Create workshop. Get // pid is project Id. Allows project to be selected public ActionResult Add(int pid = 0) { var model = new WorkshopEditModel(); // Add list of projects and employees to choose from LoadListsForEdit(model); // If given project id (pid) exists, set ProjectId so it's selected initially if (pid > 0 && _projectService.Exists(pid)) { model.ProjectId = pid; } // Default date to today's model.TrainingDate = DateTime.Now; // Pass the action to the form so it knows to return to Add ViewData["Action"] = "Add"; return(View(model)); }
private void LoadListsForEdit(WorkshopEditModel model) { model.Employees = _employeeService.GetEmployees(); model.Projects = _projectService.GetProjects(); model.ProviderCodes = _providerCodeService.ProviderCodesForView(); }