public async Task <IActionResult> EditForumComment(string CommentId) { var comment = await(from e in DBContext.ForumComments .Include(f => f.PinnedFiles) .Include(e => e.ParentEndpoint) where e.Id.ToString() == CommentId select e).FirstOrDefaultAsync(); var model = new EditForumCommentViewModel() { CommentId = comment.Id.ToString(), EndpointId = comment.ParentEndpoint.Id.ToString(), CommentText = comment.Text.Replace("<br>", "\n"), PinnedFiles = comment.PinnedFiles, CommentCreatorEmail = comment.CreatorEmail }; return(View(model)); }
public async Task <IActionResult> EditForumComment(EditForumCommentViewModel model) { if (ModelState.IsValid) { var comment = await(from e in DBContext.ForumComments.Include(f => f.PinnedFiles) where e.Id.ToString() == model.CommentId select e).FirstOrDefaultAsync(); var parentEndpoint = await(from e in DBContext.ForumEndpoints.Include(c => c.Comments) where e.Id.ToString() == model.EndpointId select e).FirstOrDefaultAsync(); if ((model.CommentText.IndexOf("<script>") != -1) || (model.CommentText.IndexOf("</script>") != -1)) { ModelState.AddModelError("scripts", "Теги <script> запрещены!"); return(View(model)); } var safeText = model.CommentText.Replace("<script>", "");//на всякий safeText = safeText.Replace("\n", "<br>"); safeText += "<br><i id='upd'>Отредактирован " + System.DateTime.Now.ToString("d") + "</i>"; DBContext.ForumComments.Update(comment).Entity.Text = safeText; if (model.CommentUploadedFiles != null) { string outfolder; if ((comment.PinnedFiles != null) && (comment.PinnedFiles.Count > 0)) { outfolder = comment.PinnedFiles[0].Path.Replace(comment.PinnedFiles[0].Name, String.Empty); } else { outfolder = environment.WebRootPath + "/ForumFiles/CFiles/" + parentEndpoint.Name + "_" + System.DateTime.Now.ToString("s").Replace(":", "-") + "/"; if (!Directory.Exists(outfolder)) { Directory.CreateDirectory(outfolder); } } foreach (IFormFile file in model.CommentUploadedFiles) { if (file.Length >= 10485760) { ModelState.AddModelError("FileTooBig", "Нельзя загружать файлы свыше 10 Мегабайт."); return(View(model)); } var outpath = outfolder + file.FileName; using (var fileStream = new FileStream(outpath, FileMode.Create)) { await file.CopyToAsync(fileStream); } var efile = new EForumFile() { Name = file.FileName, Path = outpath, TypeOfParent = 2, ForumComment = comment }; DBContext.ForumFiles.Add(efile); DBContext.ForumComments.Update(comment).Entity.PinnedFiles.Add(efile); } } await DBContext.SaveChangesAsync(); return(RedirectToAction("ForumEndpoint", new { EndpointId = model.EndpointId })); } return(View(model)); }