public ActionResult DeleteSalesAgent(AccountSalesAgentsViewModel viewModel)
        {
            if (viewModel.RemoveList != null)
            {
                // Convert the currently logged-in account id to an integer.
                int accountId = Conversion.StringToInt32(User.Identity.Name);

                // Create a collection for the results of the delete operations.
                ICollection <bool> resultList = new List <bool>();

                foreach (var salesAgentId in viewModel.RemoveList)
                {
                    resultList.Add(this.doctrineShipsServices.DeleteSalesAgent(accountId, salesAgentId));
                }

                // If any of the deletions failed, output an error message.
                if (resultList.Contains(false))
                {
                    TempData["Status"] = "Error: One or more sales agents were not removed.";
                }
                else
                {
                    TempData["Status"] = "All selected sales agents were successfully removed.";
                }
            }

            return(RedirectToAction("SalesAgents"));
        }
        public ActionResult SalesAgents()
        {
            AccountSalesAgentsViewModel viewModel = new AccountSalesAgentsViewModel();

            // Convert the currently logged-in account id to an integer.
            int accountId = Conversion.StringToInt32(User.Identity.Name);

            viewModel.SalesAgents = this.doctrineShipsServices.GetSalesAgents(accountId);

            // Set the ViewBag to the TempData status value passed from the Add & Delete methods.
            ViewBag.Status = TempData["Status"];

            return(View(viewModel));
        }
        public async Task <ActionResult> AddSalesAgent(AccountSalesAgentsViewModel viewModel)
        {
            // Convert the currently logged-in account id to an integer.
            int accountId = Conversion.StringToInt32(User.Identity.Name);

            if (ModelState.IsValid)
            {
                // Clean the passed api key.
                string cleanApiKey = Conversion.StringToSafeString(Server.HtmlEncode(viewModel.ApiKey));

                IValidationResult validationResult = await this.doctrineShipsServices.AddSalesAgent(viewModel.ApiId, cleanApiKey, accountId);

                // If the validationResult is not valid, something did not validate in the service layer.
                if (validationResult.IsValid)
                {
                    TempData["Status"] = "The sales agent was successfully added.";
                }
                else
                {
                    TempData["Status"] = "Error: The sales agent was not added, a validation error occured.<br /><b>Error Details: </b>";

                    foreach (var error in validationResult.Errors)
                    {
                        TempData["Status"] += error.Value + "<br />";
                    }
                }

                return(RedirectToAction("SalesAgents"));
            }
            else
            {
                // Re-populate the view model and return with any validation errors.
                viewModel.SalesAgents = this.doctrineShipsServices.GetSalesAgents(accountId);
                return(View("~/Views/Account/SalesAgents.cshtml", viewModel));
            }
        }