Example #1
0
        // GET: Subject/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            // Get a subject by id
            var subject = await _context.Subjects.SingleOrDefaultAsync(m => m.Id == id);

            if (subject == null)
            {
                return(NotFound());
            }

            // Map the SubjectEditVM to the Subject Model
            var model = new SubjectEditVM()
            {
                Id          = subject.Id,
                Code        = subject.Code,
                Name        = subject.Name,
                Description = subject.Description
            };

            return(View(model));
        }
Example #2
0
        public async Task <IActionResult> Edit(SubjectEditVM model)
        {
            if (ModelState.IsValid)
            {
                // Get a subject by id
                var subject = await _context.Subjects.SingleOrDefaultAsync(m => m.Id == model.Id);

                try
                {
                    if (subject == null)
                    {
                        return(NotFound());
                    }

                    // Map the SubjectEditVM to the Subject Model
                    subject.Id          = model.Id;
                    subject.Code        = model.Code;
                    subject.Name        = model.Name;
                    subject.Description = model.Description;

                    // Update a subject in the Subject Table
                    _context.Update(subject);

                    // Save all changes in the database
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!SubjectExists(subject.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            return(View(model));
        }