/// <summary>
        /// Get a list of all project role links which use the given project role group.
        /// </summary>
        private List <ProjectRoleLink> GetLinksForGroup(int projectRoleGroupId, List <TCABS_DataLibrary.Models.ProjectRoleModel> projectRoleModels)
        {
            // Get all project role links which use this project role group
            var linkModels = ProjectRoleLinkProcessor.GetProjectRoleLinksForGroup(projectRoleGroupId);

            // Create a new list of projet role links (non-model)
            List <ProjectRoleLink> links = new List <ProjectRoleLink>();

            foreach (var linkModel in linkModels)
            {
                // Create new link data
                ProjectRoleLink link = new ProjectRoleLink(linkModel);

                // Fill in the role name field within the link data
                foreach (var roleModel in projectRoleModels)
                {
                    if (link.ProjectRoleId == roleModel.ProjectRoleId)
                    {
                        link.ProjectRoleName = roleModel.Name;
                        break;
                    }
                }

                // Add link to list
                links.Add(link);
            }
            return(links);
        }
        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));
            }
        }