public async Task <ActionResult> EditBlogEntry(string id) { BlogEntry entry = null; if (id == null) { entry = new BlogEntry() { AuthorId = this.userManager.GetUserId(this.User), PublishDate = DateTimeOffset.UtcNow, Tags = new Collection <BlogEntryTag>() }; } else { entry = await this.GetByPermalink(id); if (entry == null) { return(this.NotFound()); } } var model = new EditBlogEntryViewModel() { BlogEntry = entry }; model.SelectedTagNames = model.BlogEntry.Tags.Select(t => t.Tag.Name).OrderBy(t => t).ToList(); await this.SetTagsAndAuthors(model); return(this.View(model)); }
public async Task <ActionResult> EditBlogEntry(string id, EditBlogEntryViewModel model) { if (!this.ModelState.IsValid) { await this.SetTagsAndAuthors(model); return(this.View(model)); } try { await this.addOrUpdateBlogEntryCommandHandler.HandleAsync(new AddOrUpdateBlogEntryCommand() { Entity = model.BlogEntry, Tags = model.SelectedTagNames }); } catch (BusinessRuleException ex) { this.SetErrorMessage(ex.Message); await this.SetTagsAndAuthors(model); return(this.View(model)); } this.SetSuccessMessage(Resources.SavedSuccessfully); return(this.Redirect($"/Blog/{model.BlogEntry.Url}/Edit")); }
private async Task SetTagsAndAuthors(EditBlogEntryViewModel model) { model.AllTags = await this.unitOfWork.Tags .AsNoTracking() .OrderBy(t => t.Name) .ToListAsync(); model.Authors = await this.unitOfWork.Users .AsNoTracking() .OrderBy(t => t.LastName) .ThenBy(t => t.FirstName) .ToListAsync(); if (model.BlogEntry.BlogEntryFiles == null) { model.BlogEntry.BlogEntryFiles = await this.unitOfWork.BlogEntryFiles .AsNoTracking() .Where(b => b.BlogEntryId == model.BlogEntry.Id) .ToListAsync(); } }