public async Task <IActionResult> Edit(EditTopicCommand topicCommand) { var validator = new EditTopicCommandValidator(); var results = validator.Validate(topicCommand); if (!results.IsValid) { results.AddToModelState(ModelState, null); return(View()); } var topic = await _context.Topics .Include(t => t.IdentityUser) .FirstOrDefaultAsync(m => m.Id == topicCommand.Id); if (topic == null) { return(NotFound()); } topic.SetTitle(topicCommand.Title); topic.SetDescription(topicCommand.Description); await _context.SaveChangesAsync(); return(RedirectToAction("Index")); }
public async Task <IActionResult> EditTopic(TopicViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var efc = new EditTopicCommand { Id = model.Id, NewTitle = model.Title, NewDescription = model.Description, NewShowOnHomePage = model.ShowOnHomePage }; var result = await _cp.ProcessAsync(efc); if (result.Succeeded) { _logger.LogInformation("Updated topic with id {0} with title {1} and description {2}", model.Id, model.Title, model.Description); return(RedirectToAction("ManageTopics")); } _logger.LogWarning("Unable to update topic with id {0} to title {1} and description {2}", model.Id, model.Title, model.Description); // To Do - Better Error handling ModelState.AddModelError("Error", "Unable to update topic, perhaps the title is not unique."); return(View(model)); }