public async Task <IActionResult> Save(WorkFormViewModel workForm)
        {
            if (!ModelState.IsValid)
            {
                return(View("WorkForm", workForm));
            }

            var workingTime = mapper.Map <WorkingTime>(workForm);

            workingTime.UserId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier));

            try
            {
                if (workingTime.Id > 0)
                {
                    await workingTimeService.UpdateAsync(workingTime);
                }
                else
                {
                    await workingTimeService.CreateAsync(workingTime);
                }

                TempData[EventsConstants.Success] = "Registro de trabalho gravado com sucesso!";
                return(RedirectToAction("Index"));
            }
            catch (ValidationException e)
            {
                ModelState.AddModelError(e.Property, e.Message);
                return(View("WorkForm", workForm));
            }
        }
Exemple #2
0
        public ActionResult Create()
        {
            var viewModel = new WorkFormViewModel()
            {
                TaskNames = _context.TaskNames.ToList()  // avoid null reference exception cause taskNames not populated
            };

            return(View(viewModel));
        }
Exemple #3
0
        public ActionResult Create(WorkFormViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.TaskNames = _context.TaskNames.ToList(); // avoid null reference exception cause taskNames not populated
                return(View("Create", model));
            }

            var work = new Work()
            {
                TeamLeadId = User.Identity.GetUserId(),
                Location   = model.Location,
                DateTime   = model.GetDateTime(),
                TaskNameId = model.TaskName
            };

            _context.Works.Add(work);
            _context.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }