Пример #1
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                var lib = new UserLib();
                if (lib.CheckUserName(model.UserName))
                {
                    ModelState.AddModelError("", "The account has existed !");
                }
                else if (lib.CheckEmail(model.Email))
                {
                    ModelState.AddModelError("", "Email has existed");
                }
                else
                {
                    var user = new User();
                    user.UserName    = model.UserName;
                    user.Name        = model.Name;
                    user.Password    = Encryptor.MD5Hash(model.Password);
                    user.Phone       = model.Phone;
                    user.Email       = model.Email;
                    user.Address     = model.Address;
                    user.CreatedDate = DateTime.Now;
                    user.Status      = true;
                    if (!string.IsNullOrEmpty(model.ProvinceID))
                    {
                        user.ProvinceID = int.Parse(model.ProvinceID);
                    }
                    if (!string.IsNullOrEmpty(model.DistrictID))
                    {
                        user.DistrictID = int.Parse(model.DistrictID);
                    }

                    var result = lib.Insert(user);
                    if (result > 0)
                    {
                        ViewBag.Success = "Register Successfully";
                        model           = new RegisterModel();
                    }
                    else
                    {
                        ModelState.AddModelError("", "Register Unsuccessfully.");
                    }
                }
            }
            return(View(model));
        }
        public ActionResult Create(User user)
        {
            if (ModelState.IsValid)
            {
                var lib             = new UserLib();
                var encryptedMd5Pas = Encryptor.MD5Hash(user.Password);
                user.Password = encryptedMd5Pas;

                long id = lib.Insert(user);
                if (id > 0)
                {
                    SetAlert("Add user successfully ", "success");
                    return(RedirectToAction("Index", "User"));
                }
                else
                {
                    ModelState.AddModelError("", "Add user successfully");
                }
            }
            return(View("Index"));
        }