Пример #1
0
        public ActionResult Create(RoleModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    ModelState.AddModelError("", "Please input role name");
                    return View(model);
                }

                if (!model.SelectedPermissions.Any())
                {
                    ModelState.AddModelError("", "Please select some permssions.");
                    return View(model);
                }

                bool isSuccess = RoleManager.Instance.Insert(model.RoleName, model.SelectedPermissions);
                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Error hanppen, check your input.");
                    return View(model);
                }

                return RedirectToAction("index");
            }
            catch (Exception ex)
            {
                TempData["Error"] = ex.Message;
                return View("Error");
            }
        }
Пример #2
0
        public ActionResult Edit(int id)
        {
            try
            {
                if (id <= 0)
                {
                    return RedirectToAction("index");
                }

                var role = RoleManager.Instance.LoadById(id);
                if (role == null)
                {
                    TempData["Error"] = "This record doesn't exists.";
                    return View("Error");
                }
                var model = new RoleModel
                {
                   RoleId = id,
                   RoleName = role.Name,
                   SelectedPermissions = role.Permissions
                };

                return View(model);
            }
            catch (Exception ex)
            {
                TempData["Error"] = ex.Message;
                return View("Error");
            }
        }
Пример #3
0
 public ActionResult Create()
 {
     var model = new RoleModel();
     return View(model);
 }
Пример #4
0
        public ActionResult Edit(RoleModel model)
        {
            if (model == null)
            {
                TempData["Error"] = "The post data is null";
                return View("Error");
            }
            if (!ModelState.IsValid)
            {
                ModelState.AddModelError("", "Please input role name");
                return View(model);
            }

            if (!model.SelectedPermissions.Any())
            {
                ModelState.AddModelError("", "Please select some permssions.");
                return View(model);
            }
            try
            {
                var role = new Role
                {
                    RoleId = model.RoleId,
                    Name = model.RoleName,
                    Permissions = model.SelectedPermissions
                };
                bool isSuccess = RoleManager.Instance.Update(role);
                if (!isSuccess)
                {
                    ModelState.AddModelError("", "Error hanppen, check your input.");
                    return View(model);
                }

                return RedirectToAction("index");
            }
            catch (Exception ex)
            {
                TempData["Error"] = ex.Message;
                return View("Error");
            }
        }