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

            if (ModelState.IsValid)
            {
                IValidationResult validationResult = this.doctrineShipsServices.UpdateDoctrineShipFits(accountId, viewModel.DoctrineId, viewModel.DoctrineShipFitIds);

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

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

                return(RedirectToAction("Doctrines"));
            }
            else
            {
                // Re-populate the view model and return with any validation errors.
                viewModel.Doctrines = this.doctrineShipsServices.GetDoctrineList(accountId);
                ViewBag.Status      = "Error: The doctrine ship fit list was not updated, a validation error occured.";
                return(View("~/Views/Account/Doctrines.cshtml", viewModel));
            }
        }
        public ActionResult DeleteDoctrine(AccountDoctrinesViewModel 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 doctrineId in viewModel.RemoveList)
                {
                    resultList.Add(this.doctrineShipsServices.DeleteDoctrine(accountId, doctrineId));
                }

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

            return(RedirectToAction("Doctrines"));
        }
        public ActionResult Doctrines()
        {
            AccountDoctrinesViewModel viewModel = new AccountDoctrinesViewModel();

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

            viewModel.Doctrines = this.doctrineShipsServices.GetDoctrineList(accountId).OrderBy(x => x.DoctrineId);

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

            return(View(viewModel));
        }
        public ActionResult UpdateDoctrine(AccountDoctrinesViewModel viewModel)
        {
            // Convert the currently logged-in account id to an integer.
            int accountId = Conversion.StringToInt32(User.Identity.Name);

            if (ModelState.IsValid)
            {
                // Create an Auto Mapper map between the doctrine entity and the view model.
                Mapper.CreateMap <AccountDoctrinesViewModel, Doctrine>();

                // Sanitise the form values.
                viewModel.AccountId   = accountId;
                viewModel.Name        = Conversion.StringToSafeString(viewModel.Name);
                viewModel.Description = viewModel.Description;
                viewModel.ImageUrl    = Server.HtmlEncode(viewModel.ImageUrl) ?? string.Empty;

                // Populate a doctrine with automapper and pass it back to the service layer for update.
                Doctrine          doctrine         = Mapper.Map <AccountDoctrinesViewModel, Doctrine>(viewModel);
                IValidationResult validationResult = this.doctrineShipsServices.UpdateDoctrine(doctrine);

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

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

                return(RedirectToAction("Doctrines"));
            }
            else
            {
                // Re-populate the view model and return with any validation errors.
                viewModel.Doctrines = this.doctrineShipsServices.GetDoctrineList(accountId);
                ViewBag.Status      = "Error: The doctrine was not updated, a validation error occured.";
                return(View("~/Views/Account/Doctrines.cshtml", viewModel));
            }
        }