コード例 #1
0
        public async Task <IActionResult> Create([Bind("ID,Title,DaysPerWeek,NumberOfWeeks")] Workout_Planner.Models.Workout.Program program)
        {
            if (ModelState.IsValid)
            {
                _context.Add(program);

                await _context.SaveChangesAsync();

                CreateProgramWeeksForProgram(program.NumberOfWeeks, program.ID);

                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(program));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("ID,Title,DaysPerWeek,NumberOfWeeks")] Workout_Planner.Models.Workout.Program program)
        {
            if (id != program.ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var prevNumWeeks =
                        (from w in _context.ProgramWeek
                         where (w.ProgramID == program.ID)
                         select w).Count();
                    // Does not work!
                    //_context.Entry(program).OriginalValues.GetValue<int>("NumberOfWeeks");
                    UpdateProgramWeeks(program.ID,
                                       prevNumWeeks,
                                       program.NumberOfWeeks);
                    _context.Update(program);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ProgramExists(program.ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(program));
        }