public IActionResult CreateForumEndpoint(string ParentNodeId, string CreatorEmail) { var p = ParentNodeId; var e = CreatorEmail; var model = new CreateForumEndpointViewModel() { ParentNodeId = p, CreatorEmail = e, }; return(View(model)); }
public async Task <IActionResult> CreateForumEndpoint(CreateForumEndpointViewModel model) { if (ModelState.IsValid) { var creator = await(from u in DBContext.Users where u.NormalizedEmail == model.CreatorEmail.ToUpper() select u).FirstOrDefaultAsync(); var parentNode = await(from n in DBContext.ForumNodes.Include(n => n.ChildEndpoints) where n.Id.ToString() == model.ParentNodeId select n).FirstOrDefaultAsync(); if ((model.EndpointText.IndexOf("<script>") != -1) || (model.EndpointText.IndexOf("</script>") != -1)) { ModelState.AddModelError("scripts", "Теги <script> запрещены!"); return(View(model)); } var safeText = model.EndpointText.Replace("<script>", "");//на всякий safeText = safeText.Replace("\n", "<br>"); var NewEndpoint = new EForumEndpoint() { CreatorId = creator.Id, CreatorEmail = creator.Email, CreatorFio = creator.FIO, ParentNode = parentNode, CreationDate = System.DateTime.Now, Name = model.EndpointName, Text = safeText,//--- PinnedFiles = new List <EForumFile>() }; // DBContext.ForumEndpoints.Add(NewEndpoint); // await DBContext.SaveChangesAsync(); if (model.UploadedFiles != null) { string outfolder = environment.WebRootPath + "/ForumFiles/EPFiles/" + model.EndpointName + "_" + System.DateTime.Now.ToString("s").Replace(":", "-") + "/"; if (!Directory.Exists(outfolder)) { Directory.CreateDirectory(outfolder); } foreach (IFormFile file in model.UploadedFiles) { 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 = 1, ForumEndpoint = NewEndpoint }; DBContext.ForumFiles.Add(efile); NewEndpoint.PinnedFiles.Add(efile); } } DBContext.ForumEndpoints.Add(NewEndpoint); await DBContext.SaveChangesAsync(); //return RedirectToAction("ForumNode", new { NodeId = model.ParentNodeId }); return(RedirectToAction("ForumEndpoint", new { EndpointId = NewEndpoint.Id })); } return(View(model)); }