public ActionResult Index()
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.ProjectRole))
            {
                return(RedirectToPermissionDenied());
            }

            // Set the page message
            ViewBag.Message = "Project Role List";

            // Get all project roles from the database
            var projectRoleModels = ProjectRoleProcessor.GetAllProjectRoles();

            // Convert the list to the correct type
            List <ProjectRole> projectRoles = new List <ProjectRole>();

            foreach (var p in projectRoleModels)
            {
                projectRoles.Add(new ProjectRole(p));
            }

            // Return the view, with the list of project roles
            return(View(projectRoles));
        }
        public ActionResult Edit(int?id)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.ProjectRoleGroup))
            {
                return(RedirectToPermissionDenied());
            }

            // Check if a project role group ID was provided
            if (id == null)
            {
                return(RedirectToIndex());
            }
            int projectRoleGroupId = (int)id;

            // Entry try-catch from here
            // Make sure any errors are displayed
            try
            {
                // Get the project role group data
                var projectRoleGroupModel = ProjectRoleGroupProcessor.GetProjectRoleGroup(projectRoleGroupId);
                if (projectRoleGroupModel == null)
                {
                    return(RedirectToIndexIdNotFound(projectRoleGroupId));
                }

                // Convert the model data to non-model data
                ProjectRoleGroup projectRoleGroup = new ProjectRoleGroup(projectRoleGroupModel);

                // Get list of all project roles
                var projectRoleModels = ProjectRoleProcessor.GetAllProjectRoles();

                // Get the links which are assigned to this group
                projectRoleGroup.Links = GetLinksForGroup(projectRoleGroup.ProjectRoleGroupId, projectRoleModels);

                // Setup the dropdown list with all project roles
                SetupRoleDropDownList(projectRoleModels);

                // Pass the data to the view
                return(View(projectRoleGroup));
            }
            catch (Exception e)
            {
                return(RedirectToIndex(e));
            }
        }
        public ActionResult Edit(ProjectRoleGroup projectRoleGroup)
        {
            // Make sure the user is logged in and that they have permission
            if (!IsUserLoggedIn)
            {
                return(RedirectToLogin());
            }
            if (!UserHasPermission(PermissionName.ProjectRoleGroup))
            {
                return(RedirectToPermissionDenied());
            }

            // Enter main body of the function
            try
            {
                // Check which submit button was pressed
                if (Request.Form["Save"] != null)
                {
                    // Make sure the user is logged in
                    if (!IsUserLoggedIn)
                    {
                        return(RedirectToLogin());
                    }

                    // Make sure the entered data is valid
                    if (ModelState.IsValid)
                    {
                        // Update the project role group within the database
                        try
                        {
                            ProjectRoleGroupProcessor.UpdateProjectRoleGroup(
                                projectRoleGroup.ProjectRoleGroupId,
                                projectRoleGroup.Name);
                            TempData[LabelSuccess] = "Updated group: " + projectRoleGroup.Name;
                            return(RedirectToIndex());
                        }
                        catch (Exception e)
                        {
                            ModelState.AddModelError("", e.Message);
                        }
                    }
                    else
                    {
                        //show error
                        var errors = ModelState.Values.SelectMany(v => v.Errors);
                    }
                }
                else if (Request.Form["Add"] != null)
                {
                    // Add a role link the project role group
                    try
                    {
                        ProjectRoleLinkProcessor.CreateProjectRoleLink(
                            projectRoleGroup.AddLink,
                            projectRoleGroup.ProjectRoleGroupId);
                        ViewBag.Added = "Added project role to group";
                    }
                    catch (Exception e)
                    {
                        ViewBag.AddFail = e.Message;
                    }
                }
                else
                {
                    // Remove a role link the project role group
                    try
                    {
                        const string marker = "Delete.";
                        foreach (string key in Request.Form.AllKeys)
                        {
                            if (key.StartsWith(marker))
                            {
                                ProjectRoleLinkProcessor.DeleteProjectRoleLink(Convert.ToInt32(key.Substring(marker.Length)));
                                ViewBag.Removed = "Removed project role from group";
                                break;
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        ViewBag.RemoveFail = e.Message;
                    }
                }

                // Get the project role group data
                var projectRoleGroupModel = ProjectRoleGroupProcessor.GetProjectRoleGroup(projectRoleGroup.ProjectRoleGroupId);
                if (projectRoleGroupModel == null)
                {
                    return(RedirectToIndexIdNotFound(projectRoleGroup.ProjectRoleGroupId));
                }

                // Convert the model data to non-model data
                projectRoleGroup = new ProjectRoleGroup(projectRoleGroupModel);

                // Get list of all project roles
                var projectRoleModels = ProjectRoleProcessor.GetAllProjectRoles();

                // Get the links which are assigned to this group
                projectRoleGroup.Links = GetLinksForGroup(projectRoleGroup.ProjectRoleGroupId, projectRoleModels);

                // Setup the dropdown list with all project roles
                SetupRoleDropDownList(projectRoleModels);

                // Return to same page with same data
                return(View(projectRoleGroup));
            }
            catch (Exception e)
            {
                return(RedirectToIndex(e));
            }
        }