コード例 #1
0
 public ActionResult Edit(User user)
 {
     try
     {
         if (ModelState.IsValid)
         {
             // TODO: Add update logic here
             using (UsersConfigModel usersConfigModel = new UsersConfigModel())
             {
                 user.Password = EncryptData(user.strPassword);
                 //usersConfigModel.Users.AddOrUpdate(user);
                 usersConfigModel.Entry(user).State = EntityState.Modified;
                 usersConfigModel.SaveChanges();
                 return(RedirectToAction("Index"));
             }
         }
         else
         {
             return(View(user));
         }
     }
     catch (Exception ex)
     {
         ViewBag.ErrorMessage = ex.Message;
         return(View());
     }
 }
コード例 #2
0
        public ActionResult Create(User user)
        {
            try
            {
                using (UsersConfigModel usersConfigModel = new UsersConfigModel())
                {
                    //byte[] encryptedPassword = EncryptData(user.strPassword);
                    //user.Password = encryptedPassword;
                    var isExists = usersConfigModel.Users.Any(x => x.UserName == user.UserName);
                    if (!isExists)
                    {
                        user.Password = EncryptData(user.strPassword);
                        usersConfigModel.Users.Add(user);
                        usersConfigModel.SaveChanges();
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        ViewBag.ErrorMessage = "User exists!";
                    }
                }

                return(View());
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var eve in ex.EntityValidationErrors)
                {
                    //Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
                    foreach (var ve in eve.ValidationErrors)
                    {
                        //Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                    }
                }
                ViewBag.ErrorMessage = $"Registration failed! ({ex.Message})";
                return(View());
            }
            catch (Exception ex)
            {
                ViewBag.ErrorMessage = $"Registration failed! ({ex.Message})";
                return(View());
            }
        }