public async Task <IActionResult> OnPostAsync(string redirectTo) { if (!ModelState.IsValid) { await prepareModel(); return(Page()); } var entity = ToEntity(); entity.CreationDate = DateTime.UtcNow; entity.LastUpdateDate = DateTime.UtcNow; entity.ImageUrl = await _uploadManager.SavePostImageAsync(ImageFile); await _dbContext.Posts.AddAsync(entity); await _dbContext.SaveChangesAsync(); this.InformUser(FormResult.Added, Title, "post"); if (redirectTo == "Edit") { return(RedirectToPage("Edit", new { id = entity.Id })); } else { return(RedirectToPage("Index")); } }
public async Task <IActionResult> OnPostAsync(string redirectTo) { if (!ModelState.IsValid) { await prepareModelSelectLists(); return(Page()); } var entity = await _dbContext.Posts .Include(x => x.CategoryPosts) .Where(x => x.Id == Id) .SingleOrDefaultAsync(); if (entity == null) { return(NotFound()); } //Delete post categories and insert them again _dbContext.Categories_Posts.RemoveRange(entity.CategoryPosts); //Update entity with entries in View Model. UpdateEntity(entity); entity.LastUpdateDate = DateTime.UtcNow; //Delete old image if (ImageFile != null && ImageFile.Length > 0) { _uploadManager.RemoveFile(entity.ImageUrl); entity.ImageUrl = await _uploadManager.SavePostImageAsync(ImageFile); } await _dbContext.SaveChangesAsync(); this.InformUser(FormResult.Updated, Title, "posts"); if (redirectTo == "Edit") { return(RedirectToPage("Edit", new { id = entity.Id })); } else { return(RedirectToPage("Index")); } }