public async Task <IActionResult> Attach(Int32 topicId, int messageId, [FromForm] FormFileCollection attachments) { var message = await context.ForumMessages .Include(m => m.ForumTopic) .SingleOrDefaultAsync(m => m.Id == messageId); if (message is null) { return(NotFound()); } var authResult = await authorizationService.AuthorizeAsync(User, message.Id, "MessageAuthor"); if (!authResult.Succeeded) { return(Unauthorized()); } if (!attachments.Any()) { ModelState.AddModelError("attachments", "Attachments cannot be empty."); return(View(message)); } var files = new List <ForumMessageAttachment>(); foreach (var attachment in attachments) { files.Add(new ForumMessageAttachment { Created = DateTime.Now, FileName = attachment.FileName, FilePath = await CreateFile(attachment) }); } try { message.Attachments = files; context.Entry(message).Collection(e => e.Attachments).IsModified = true; await context.SaveChangesAsync(); } catch (Exception) { throw; } return(RedirectToActionPermanent("Index", new { topicId })); }