Exemple #1
0
        // GET: Steps/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {//Method to edi the step whose id matched the parameter "id"
            if (id == null)
            {
                return(NotFound());
            }
            //prepopulate the form fields with the database values that have already been saved for this specific step
            var step = await _context.Steps.FindAsync(id);

            if (step == null)
            {//if the id isn't found, then return NOtFOund()
                return(NotFound());
            }

            var stepTypes = await _context.StepTypes.ToListAsync();

            //instantiate view model
            var viewModel = new EditStepsViewModel()
            {//set up drop down to display and select step type names
                Step            = step,
                StepTypeOptions = stepTypes.Select(s => new SelectListItem
                {
                    Value = s.Id.ToString(),
                    Text  = s.StepTypeName
                }).ToList()
            };

            return(View(viewModel));
        }
Exemple #2
0
        public async Task <IActionResult> Edit(int id, EditStepsViewModel viewModel)
        {
            var step = viewModel.Step;

            if (id != step.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {//if the model state is valid, then the updates will be saved to the specific step's table in the database
                try
                {
                    var currentUser = await GetCurrentUserAsync();

                    step.UserId = currentUser.Id;
                    _context.Update(step);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!StepExists(step.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }

            var stepTypes = await _context.StepTypes.ToListAsync();

            viewModel.StepTypeOptions = stepTypes.Select(s => new SelectListItem
            {
                Value = s.Id.ToString(),
                Text  = s.StepTypeName
            }).ToList();

            return(View(viewModel));
        }