예제 #1
0
        public async Task <IActionResult> OnPostAsync(int?id)
        {
            string userId = _userManager.GetUserId(User);

            Subject subjectToEdit = await _context.Subjects
                                    .Where(su => su.UserID == userId)
                                    .Where(su => su.ID == id)
                                    .SingleOrDefaultAsync();

            if (subjectToEdit == null)
            {
                DangerMessage = "Subject not found.";

                return(RedirectToPage("./Index"));
            }

            SubjectVM = new StudentOrSubjectEditOrCreate();

            bool modelDidUpdate = await TryUpdateModelAsync <StudentOrSubjectEditOrCreate>(SubjectVM);

            if (ModelState.IsValid && modelDidUpdate)
            {
                List <string> otherUsedNames = await _context.Subjects
                                               .Where(su => su.ID != id)
                                               .Where(su => su.UserID == userId)
                                               .Select(su => su.Name)
                                               .ToListAsync();

                if (otherUsedNames.Contains(SubjectVM.Name))
                {
                    DangerMessage = "This Subject name is already used.";

                    return(RedirectToPage());
                }

                subjectToEdit.Name     = SubjectVM.Name;
                subjectToEdit.IsActive = SubjectVM.IsActive;

                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }

            DangerMessage = "Changes did not save correctly. Please try again.";

            return(RedirectToPage());
        }
예제 #2
0
        public async Task <IActionResult> OnPostAsync()
        {
            string userId = _userManager.GetUserId(User);

            SubjectVM = new StudentOrSubjectEditOrCreate();

            bool modelDidUpdate = await TryUpdateModelAsync <StudentOrSubjectEditOrCreate>(SubjectVM);

            if (modelDidUpdate && ModelState.IsValid)
            {
                List <string> usedNames = _context.Subjects
                                          .Where(su => su.UserID == userId)
                                          .Select(s => s.Name)
                                          .ToList();

                if (usedNames.Contains(SubjectVM.Name))
                {
                    DangerMessage = "This Subject name is already used.";

                    return(RedirectToPage());
                }

                Subject newSubject = new Subject
                {
                    UserID   = userId,
                    Name     = SubjectVM.Name,
                    IsActive = SubjectVM.IsActive
                };

                _context.Subjects.Add(newSubject);

                await _context.SaveChangesAsync();

                return(RedirectToPage("./Index"));
            }

            DangerMessage = "New Subject did not save correctly. Please try again";

            return(RedirectToPage());
        }
예제 #3
0
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            string userId = _userManager.GetUserId(User);

            Subject subjectToEdit = await _context.Subjects
                                    .Where(su => su.UserID == userId)
                                    .Where(su => su.ID == id)
                                    .SingleOrDefaultAsync();

            if (subjectToEdit == null)
            {
                DangerMessage = "Subject not found.";

                return(RedirectToPage("./Index"));
            }

            SubjectVM = new StudentOrSubjectEditOrCreate()
            {
                Name     = subjectToEdit.Name,
                IsActive = subjectToEdit.IsActive
            };

            return(Page());
        }