コード例 #1
0
 public void SaveUser(User user)
 {
     if (user.ID == 0)
     {
         context.Users.Add(user);
     }
     else
     {
         User dbEntry = context.Users.Find(user.ID);
         if (dbEntry != null)
         {
             dbEntry.Username = user.Username;
             dbEntry.Password = user.Password;
             dbEntry.Email = user.Email;
         }
     }
     context.SaveChanges();
 }
コード例 #2
0
        public ViewResult Register(UserRegistrationModel model,string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var existUser = _repository.Users.FirstOrDefault(item => item.Username.ToLower() == model.Username.ToLower());
                if (existUser == null)
                {
                    var user = new User {Username = model.Username, Password = model.Password, Email = model.Email};

                    _repository.SaveUser(user);

                    ViewBag.returnUrl = returnUrl;
                    return View("RegistrationCompleted");
                }

                ModelState.AddModelError("", "An User with same User name already exists");

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