예제 #1
0
        public ActionResult Create([Bind(Include = "ID,UserName,Password,Active")] User user)
        {
            if (ModelState.IsValid)
            {
                using (var dbContextTransaction = _db.Database.BeginTransaction())
                {
                    try
                    {
                        string encodedPassword = TextEncoding.EncodeString(user.Password);
                        user.Password = encodedPassword;
                        user          = _db.Users.Add(user);
                        _db.SaveChanges();

                        dbContextTransaction.Commit();


                        return(RedirectToAction("Index"));
                    }
                    catch (Exception ex)
                    {
                        dbContextTransaction.Rollback();
                    }
                }
            }

            return(View(user));
        }
        public ActionResult Create([Bind(Include = "Id,UserId,RoleId")] UserRole user_Roles)
        {
            if (ModelState.IsValid)
            {
                _db.UserRoles.Add(user_Roles);
                _db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.RoleId = new SelectList(_db.Roles, "ID", "Roles", user_Roles.RoleId);
            ViewBag.UserId = new SelectList(_db.Users, "ID", "UserName", user_Roles.UserId);
            return(View(user_Roles));
        }