コード例 #1
0
        public async Task <IActionResult> Edit(int id, EditPersoonViewModel viewModel)
        {
            if (id != viewModel.Persoon.Persoon_ID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(viewModel.Persoon);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!PersoonExists(viewModel.Persoon.Persoon_ID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
            }
            return(RedirectToAction(nameof(Index)));
        }
コード例 #2
0
        // GET: Persoon/Edit/5
        public async Task <IActionResult> Edit(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            EditPersoonViewModel viewModel = new EditPersoonViewModel();

            viewModel.Persoon = await _context.Personen.Include(x => x.CustomUser).FirstOrDefaultAsync(x => x.Persoon_ID == id);

            viewModel.Persoon.UserID        = viewModel.Persoon.CustomUser.Id;
            viewModel.Persoon.Geboortedatum = viewModel.Persoon.Geboortedatum;
            if (viewModel.Persoon == null)
            {
                return(NotFound());
            }
            return(View(viewModel));
        }
コード例 #3
0
        public ActionResult SpelerIsAdmin(EditPersoonViewModel viewModel, int PersoonID)
        {
            viewModel.Persoon         = _context.Personen.Include(x => x.CustomUser).FirstOrDefault(x => x.Persoon_ID == PersoonID);
            viewModel.Persoon.IsAdmin = !viewModel.Persoon.IsAdmin;
            if (viewModel.Persoon.IsAdmin == true)
            {
                viewModel.Persoon.RolDuiding = "Administrator";

                DbSet <IdentityUserRole <string> > roles = _context.UserRoles;
                IdentityRole userrole = _context.Roles.FirstOrDefault(r => r.Name == "Speler");
                if (userrole != null)
                {
                    if (!roles.Any(ur => ur.UserId == viewModel.Persoon.CustomUser.Id && ur.RoleId == userrole.Id))
                    {
                        roles.Add(new IdentityUserRole <string>()
                        {
                            UserId = viewModel.Persoon.CustomUser.Id, RoleId = userrole.Id
                        });
                        _context.SaveChanges();
                    }
                }
            }
            if (viewModel.Persoon.IsAdmin == false)
            {
                viewModel.Persoon.RolDuiding = "Speler";
                DbSet <IdentityUserRole <string> > roles = _context.UserRoles;
                IdentityRole userrole = _context.Roles.FirstOrDefault(r => r.Name == "Speler");
                if (userrole != null)
                {
                    if (!roles.Any(ur => ur.UserId == viewModel.Persoon.CustomUser.Id && ur.RoleId == userrole.Id))
                    {
                        roles.Add(new IdentityUserRole <string>()
                        {
                            UserId = viewModel.Persoon.CustomUser.Id, RoleId = userrole.Id
                        });
                        _context.SaveChanges();
                    }
                }
            }
            return(View("Edit", viewModel));
        }