public async Task <PartialViewResult> NewPosition() { var categories = new List <Category>(); var client = new Client(); var response = await this.GetHttpClient().GetAsync("Category"); // Get all categories if (response.IsSuccessStatusCode) { categories = await response.Content.ReadAsAsync <List <Category> >(); } else { var stringBuilder = new StringBuilder(); stringBuilder.Append("<div class='text-center'><h4><strong>Failed to retrieve the list of categories.</strong></h4></div>"); stringBuilder.Append(String.Format("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Server returned status code '{0}' while attempting to retrieve the list of categories. Please try again in a moment.</div>", response.StatusCode)); stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'><strong>NOTE:</strong> If you encounter this issue again in the future, please contact Technical Support with exact steps to reproduce this issue.</div></div>"); var confirmationViewModel = new ConfirmationViewModel(stringBuilder.ToString()); return(PartialView("_Error", confirmationViewModel)); } response = await this.GetHttpClient().GetAsync(String.Format("Client?userName={0}", User.Identity.Name)); // Get current user (Client) if (response.IsSuccessStatusCode) { client = await response.Content.ReadAsAsync <Client>(); } else { var stringBuilder = new StringBuilder(); stringBuilder.Append("<div class='text-center'><h4><strong>Failed to retrieve client's information.</strong></h4></div>"); stringBuilder.Append(String.Format("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Server returned status code '{0}' while attempting to retrieve client's information. Please try again in a moment.</div>", response.StatusCode)); stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'><strong>NOTE:</strong> If you encounter this issue again in the future, please contact Technical Support with exact steps to reproduce this issue.</div></div>"); var confirmationViewModel = new ConfirmationViewModel(stringBuilder.ToString()); return(PartialView("_Error", confirmationViewModel)); } var positionAddViewModel = new PositionAddViewModel(); // Populate client id positionAddViewModel.ClientId = client.ClientId; // Populate select list for categories positionAddViewModel.Categories = new[] { new SelectListItem { Text = "", Value = string.Empty } }.Concat(categories.Select(c => new SelectListItem { Text = c.Name, Value = c.CategoryId.ToString() }).ToList()).ToList(); return(PartialView("_NewPosition", positionAddViewModel)); }
private void OpenEditor(PositionAddViewModel dataContext) { m_EditPosition = dataContext; Dialogs.ShowDialog(m_EditPosition); }
public async Task <PartialViewResult> NewPosition(PositionAddViewModel model) { model.DateCreated = DateTime.UtcNow; var response = new HttpResponseMessage(); var categories = new List <Category>(); response = await this.GetHttpClient().GetAsync("Category"); // Get all categories if (response.IsSuccessStatusCode) { categories = await response.Content.ReadAsAsync <List <Category> >(); } else { var stringBuilder = new StringBuilder(); stringBuilder.Append("<div class='text-center'><h4><strong>Failed to retrieve the list of categories.</strong></h4></div>"); stringBuilder.Append(String.Format("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Server returned status code '{0}' while attempting to retrieve the list of categories. Please try again in a moment.</div>", response.StatusCode)); stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'><strong>NOTE:</strong> If you encounter this issue again in the future, please contact Technical Support with exact steps to reproduce this issue.</div></div>"); var confirmationViewModel = new ConfirmationViewModel(stringBuilder.ToString()); return(PartialView("_Error", confirmationViewModel)); } // Perform manual model validation if (model.AcceptanceScore == 0 && model.ClientId == 0) { ModelState.AddModelError(String.Empty, "Model state is not valid. Please try again in a moment"); } else if (model.AcceptanceScore == 0) { ModelState.AddModelError(String.Empty, "Initial Acceptance Score could not be determined. Please try again in a moment"); } else if (model.ClientId == 0) { ModelState.AddModelError(String.Empty, "Client could not be determined. Please try again in a moment"); } if (model.ExpiryDate < DateTime.Now) { ModelState.AddModelError("ExpiryDate", "The Expiry Date field cannot be set to the current or past date."); } if (model.PeopleNeeded < 1) { ModelState.AddModelError("PeopleNeeded", "The People Needed field is required and should be greater than 0."); } if (model.CategoryId == 0) { ModelState.AddModelError("CategoryId", "The Category field is required."); } if (ModelState.IsValid) { var stringBuilder = new StringBuilder(); try { var position = Mapper.Map <PositionAddViewModel, Position>(model); response = await this.GetHttpClient().PostAsJsonAsync("Position", position); // Attempt to persist position to the data context if (response.IsSuccessStatusCode) { position = await response.Content.ReadAsAsync <Position>(); var notification = Mapper.Map <NotificationAddViewModel, Notification>(new NotificationAddViewModel("New Position Confirmation", String.Format("Position '{0}' has been successfully created and sent for approval.", position.Title), null, position.ClientId)); response = await this.GetHttpClient().PostAsJsonAsync("Notification", notification); // Attempt to persist notification to the data context if (!response.IsSuccessStatusCode) { throw new NotificationAddException(String.Format("Notification could NOT be added. Status Code: {1}", response.StatusCode)); } position.Category = categories.FirstOrDefault(c => c.CategoryId == position.CategoryId); var positionConfirmationViewModel = Mapper.Map <Position, PositionConfirmationViewModel>(position); return(PartialView("_PositionConfirmation", positionConfirmationViewModel)); } else { // If position could not be created, throw PositionAddException exception throw new PositionAddException("Position " + position.Title + " could not be created. Response: " + response.StatusCode); } } catch (PositionAddException ex) { // Log exception ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex)); stringBuilder.Append("<div class='text-center'><h4><strong>Failed to save position details.</strong></h4></div>"); stringBuilder.Append(String.Format("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>Server returned status code '{0}' while attempting to persist position details to the database. Please try again in a moment.</div>", response.StatusCode)); stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'><strong>NOTE:</strong> If you encounter this issue again in the future, please contact Technical Support with exact steps to reproduce this issue.</div></div>"); var confirmationViewModel = new ConfirmationViewModel(stringBuilder.ToString()); return(PartialView("_Error", confirmationViewModel)); } catch (NotificationAddException ex) { // Log exception ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex)); stringBuilder.Append("<div class='text-center'><h4><strong>Failed to create new notification.</strong></h4></div>"); stringBuilder.Append(String.Format("<div class='row'><div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'>An exception has been thrown while attempting to create new notification.</div>")); stringBuilder.Append("<div class='col-md-12'><p></p></div><div class='col-md-offset-1 col-md-11'><strong>NOTE:</strong> Please review an exception log for more information about the exception.</div></div>"); var confirmationViewModel = new ConfirmationViewModel(stringBuilder.ToString()); return(PartialView("_Error", confirmationViewModel)); } } // If model state is not valid, re-populate Categories select list and redisplay the form model.Categories = new[] { new SelectListItem { Text = "", Value = string.Empty } }.Concat(categories.Select(c => new SelectListItem { Text = c.Name, Value = c.CategoryId.ToString() }).ToList()).ToList(); return(PartialView("_NewPosition", model)); }