Exemple #1
0
        public ActionResult Add([Bind(Include = "Title,Genre,YearPublished,Price,UserId")] UserComputerGame userComputerGame)
        {
            if (ModelState.IsValid && ValidateUserComputerGame(userComputerGame))
            {
                userComputerGame.UserId = (int)Session["CurrentUserId"];
                db.UserComputerGames.Add(userComputerGame);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View("Add", userComputerGame));
        }
        public ActionResult Register(UserModel userModel)
        {
            if (IsUsernameTaken(userModel.Username))
            {
                ModelState.AddModelError("", "Username is taken");
                return(View("Register"));
            }

            if (ValidateInputs(userModel))
            {
                // Password is hashed for extra security
                string hashedPassword = Crypto.HashPassword(userModel.Password);
                User   user           = new User
                {
                    Username       = userModel.Username,
                    HashedPassword = hashedPassword
                };

                using (ComputerGamesLibraryContext context = new ComputerGamesLibraryContext())
                {
                    context.Users.Add(user);
                    context.SaveChanges();
                }

                return(RedirectToAction("Login", "Accounts"));
            }
            else
            {
                return(View("Register", userModel));
            }
        }