Ejemplo n.º 1
0
        public ActionResult UserAdd()
        {
            Account model = new Account();

            IEnumerable<SelectListItem> categories = _employeeRepositoryAsync.Query().Select().ToList()
                .Select(s => new SelectListItem
                {
                    Value = s.Id.ToString(),
                    Text = s.Fullname
                });
            ViewBag.EmployeeId = new SelectList(categories, "Value", "Text");

            IEnumerable<SelectListItem> roles = _roleRepositoryAsync.Query().Select().ToList()
                .Select(s => new SelectListItem
                {
                    Value = s.Id.ToString(),
                    Text = s.RoleType
                });
            ViewBag.RoleId = new SelectList(roles, "Value", "Text");

            return View("Edit", model);
        }
Ejemplo n.º 2
0
        public ActionResult UserEdit(Account model)
        {
            IEnumerable<SelectListItem> categories = _employeeRepositoryAsync.Query().Select().ToList()
                .Select(s => new SelectListItem
                {
                    Value = s.Id.ToString(),
                    Text = s.Fullname
                });
            ViewBag.EmployeeId = new SelectList(categories, "Value", "Text");

            IEnumerable<SelectListItem> roles = _roleRepositoryAsync.Query().Select().ToList()
                .Select(s => new SelectListItem
                {
                    Value = s.Id.ToString(),
                    Text = s.RoleType
                });
            ViewBag.RoleId = new SelectList(roles, "Value", "Text");

            if (ModelState.IsValid)
            {
                if (model.Id == 0)
                {
                    var account = _accountRepositoryAsync
                        .Query(e => e.Username == model.Username)
                        .Include(r => r.Role)
                        .Select()
                        .SingleOrDefault();

                    if (account != null)
                    {
                        ModelState.AddModelError("Username", "Username already exists!");
                    }
                    else
                    {
                        var password = AccountHelpers.HashPassword(model.Password);
                        model.Salt = password.Salt;
                        model.Password = password.HashPassword;
                        _accountRepositoryAsync.Insert(model);
                        _unitOfWorkAsync.SaveChanges();
                        return RedirectToAction("Index", "User");
                    }
                }
                else
                {
                    var account = _accountRepositoryAsync
                        .Query(e => e.Username == model.Username)
                        .Include(r => r.Role)
                        .Select()
                        .SingleOrDefault();

                    if (account != null)
                    {
                        if (account.Id != model.Id)
                        {
                            ModelState.AddModelError("Username", "Username already exists!");
                        }
                    }
                    else
                    {
                        if (model.Password != "")
                        {
                            var password = AccountHelpers.HashPassword(model.Password);
                            model.Salt = password.Salt;
                            model.Password = password.HashPassword;
                        }
                        _accountRepositoryAsync.Update(model);
                        _unitOfWorkAsync.SaveChanges();
                        return RedirectToAction("Index", "User");
                    }
                }

            }

            return View("Edit", model);
        }