Beispiel #1
0
        public async Task <IActionResult> Edit(string id)
        {
            ViewBag.User = await GetCurrentUser();

            if (string.IsNullOrEmpty(id))
            {
                return(NotFound());
            }

            var applicationUser = await _context.ApplicationUser.SingleOrDefaultAsync(m => m.Id == id);

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

            var viewModel = new InactiveEditViewModel
            {
                Id          = applicationUser.Id,
                FirstName   = applicationUser.FirstName,
                LastName    = applicationUser.LastName,
                Street1     = applicationUser.Street1,
                Street2     = applicationUser.Street2,
                City        = applicationUser.City,
                State       = applicationUser.State,
                Zip         = applicationUser.ZipCode,
                Phone       = applicationUser.PhoneNumber,
                Email       = applicationUser.Email,
                WhenJoined  = applicationUser.WhenJoined,
                WhenExpires = applicationUser.WhenExpires
            };

            return(View(viewModel));
        }
Beispiel #2
0
        public async Task <IActionResult> Edit([Bind("Id,City,Email,FirstName,LastName,State,Street1,Street2,Zip,Phone,Email,WhenJoined,WhenExpires")] InactiveEditViewModel viewModel)
        {
            ViewBag.User = await GetCurrentUser();

            var applicationUser = await _context.ApplicationUser.SingleOrDefaultAsync(m => m.Id == viewModel.Id);

            if (viewModel.Id != applicationUser.Id)
            {
                return(NotFound());
            }

            var currentUser = _context.ApplicationUser.FirstOrDefault(a => a.Email == viewModel.Email);

            if (currentUser != null && currentUser.Id != viewModel.Id)
            {
                ModelState.AddModelError("Email", "Email already in use");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    applicationUser.Email       = viewModel.Email;
                    applicationUser.FirstName   = viewModel.FirstName;
                    applicationUser.LastName    = viewModel.LastName;
                    applicationUser.Street1     = viewModel.Street1;
                    applicationUser.Street2     = viewModel.Street2;
                    applicationUser.City        = viewModel.City;
                    applicationUser.State       = viewModel.State;
                    applicationUser.ZipCode     = viewModel.Zip;
                    applicationUser.Email       = viewModel.Email;
                    applicationUser.PhoneNumber = viewModel.Phone;
                    applicationUser.UserName    = viewModel.Email;
                    _context.Update(applicationUser);
                    await _context.SaveChangesAsync();

                    Response.Cookies.Append("FlashSuccess", "Member " + applicationUser.FirstName + " " + applicationUser.LastName + " was successfully saved");
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!ApplicationUserExists(applicationUser.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction("Index"));
            }
            return(View(viewModel));
        }