Ejemplo n.º 1
0
        // GET: Products/Edit/5
        public async Task <IActionResult> Edit(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            IEnumerable <ApplicationUser> members = ReturnAllMembers().Result;
            ApplicationUser user = (ApplicationUser)members.Single(u => u.Id == id);

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

            ViewData["userId"] = user.Id;

            var vm = new AccountViewModelBase()
            {
                Email             = user.Email,
                FirstName         = user.FirstName,
                LastName          = user.LastName,
                HomePhoneNumber   = user.HomePhoneNumber,
                WorkPhoneNumber   = user.WorkPhoneNumber,
                MobilePhoneNumber = user.MobilePhoneNumber,
                Address           = user.Address
            };

            return(View(vm));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Edit(AccountViewModelBase model, string userId)
        {
            if (userId == null)
            {
                return(NotFound());
            }
            IEnumerable <ApplicationUser> members = ReturnAllMembers().Result;
            ApplicationUser user = (ApplicationUser)members.Single(u => u.Id == userId);

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

            // check same email
            var checkUser = await _userManager.FindByEmailAsync(model.Email);

            if ((checkUser != null) && (checkUser.Id != user.Id))
            {
                ModelState.AddModelError("", "Email has been registered, please select another one.");
            }

            // validate phone
            if (!Utils.ContainValidPhoneNumber(model.HomePhoneNumber, model.WorkPhoneNumber, model.MobilePhoneNumber))
            {
                ModelState.AddModelError("", "At least one valid phone number is required.");
            }


            if (ModelState.IsValid)
            {
                user.UserName          = model.Email;
                user.Email             = model.Email;
                user.FirstName         = model.FirstName;
                user.LastName          = model.LastName;
                user.HomePhoneNumber   = model.HomePhoneNumber;
                user.WorkPhoneNumber   = model.WorkPhoneNumber;
                user.MobilePhoneNumber = model.WorkPhoneNumber;
                user.Address           = model.Address;

                await _userManager.UpdateAsync(user);

                return(RedirectToAction("Index"));
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }