示例#1
0
        public ActionResult Create([ModelBinder(typeof(RoleViewModelBinder))] RoleView RoleView)//[ModelBinder(typeof(RoleViewModelBinder))],string[] selectedAvailableFunctions
        {
            UserAccessRepository rep = new UserAccessRepository();

            // Get the list of roles in the system
            List <AvailableFunction> allFunctions = rep.GetAllAvailableFunctions();

            RoleView.Roles = rep.GetAllRoles();



            if (ModelState.IsValid)
            {
                Role newRole = new Role(Guid.NewGuid(), RoleView.Name, RoleView.Description);
                newRole.AvailableFunctions = new List <AvailableFunction>();

                for (int i = 0; i < allFunctions.Count(); i++)
                {
                    foreach (var item in RoleView.AvailableFunctions)
                    {
                        if (Guid.Parse(item.Value).Equals(allFunctions[i].ID))
                        {
                            newRole.AvailableFunctions.Add(new AvailableFunction(Guid.Parse(allFunctions[i].ID.ToString()), allFunctions[i].FunctionName));
                        }
                    }
                }

                if (rep.CreateRole(newRole) == true)
                {
                    return(RedirectToAction("Create", "Roles", new { message = "Your role '" + RoleView.Name + "' was created successfully!" }));
                }
                else
                {
                    return(RedirectToAction("Create", "Roles", new { message = "There was an error creating your role. Please contact your administrator." }));
                }
            }

            RoleView.Roles = rep.GetAllRoles();

            if (RoleView.AvailableFunctions == null)
            {
                RoleView.AvailableFunctions = new List <CheckBoxListInfo>();
            }

            RoleView.AvailableFunctions.Clear();

            foreach (var item in allFunctions)
            {
                RoleView.AvailableFunctions.Add(new CheckBoxListInfo(item.ID.ToString(), item.FunctionName, false));
            }

            return(View("Create", RoleView));
        }