Exemplo n.º 1
0
        public ActionResult Create(UserManager_CreateEditViewModel newUserVM)
        {
            //Validate password and username are defined
            if (string.IsNullOrWhiteSpace(newUserVM.UserName) == true)
            {
                ModelState.AddModelError("", "Username is required");
                return(View(newUserVM));
            }

            if (string.IsNullOrWhiteSpace(newUserVM.Password) == true)
            {
                ModelState.AddModelError("", "Password is required");
                return(View(newUserVM));
            }

            if (newUserVM.Password != newUserVM.PasswordConfirm)
            {
                ModelState.AddModelError("", "Password Confirm does not match");
                return(View(newUserVM));
            }


            //Create a user dto template
            User userTemplate = new wsad_app.Models.DataAccess.User()
            {
                EmailAddress = newUserVM.EmailAddress,
                EmailOpt     = newUserVM.EmailOpt,
                FirstName    = newUserVM.FirstName,
                LastName     = newUserVM.LastName,
                Password     = newUserVM.Password,
                UserName     = newUserVM.UserName
            };

            //Create a user manager
            UserManager userMngr = new UserManager();

            //Send new user into manager
            try
            {
                userMngr.AddUser(userTemplate);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Error occured while saving user to database. " + ex.Message);
                Debug.WriteLine(ex.Message);
                return(View(newUserVM));
            }

            //Redirect to Index
            return(RedirectToAction("Index"));
        }
Exemplo n.º 2
0
        public ActionResult Edit(UserManager_CreateEditViewModel editUserVM)
        {
            //Validate password and username are defined
            if (string.IsNullOrWhiteSpace(editUserVM.UserName) == true)
            {
                ModelState.AddModelError("", "Username is required");
                return(View(editUserVM));
            }

            if (editUserVM.Password != editUserVM.PasswordConfirm)
            {
                ModelState.AddModelError("", "Password Confirm does not match");
                return(View(editUserVM));
            }


            //Create a user dto template
            User userTemplate = new wsad_app.Models.DataAccess.User()
            {
                Id           = editUserVM.Id,
                EmailAddress = editUserVM.EmailAddress,
                EmailOpt     = editUserVM.EmailOpt,
                FirstName    = editUserVM.FirstName,
                LastName     = editUserVM.LastName,
                Password     = editUserVM.Password,
                UserName     = editUserVM.UserName
            };

            //Create a user manager
            UserManager userMngr = new UserManager();

            //Send new user into manager
            try
            {
                userMngr.UpdateUser(userTemplate);
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", "Error occured while saving user to database. " + ex.Message);
            }

            //Redirect to Index
            return(PartialView("_UserSaveSuccess", null));
        }