public async Task <IActionResult> CreateAppendix(int id, [Bind("Id,Name,FileName")] AppendixViewModel appendix) { if (ModelState.IsValid) { string fileName = ""; if (appendix.FileName != null) { string uploadPath = Path.Combine(hostingEnvironment.WebRootPath, "images"); fileName = Guid.NewGuid().ToString() + "_" + appendix.FileName.FileName; string FilePath = Path.Combine(uploadPath, fileName); appendix.FileName.CopyTo(new FileStream(FilePath, FileMode.Create)); } Appendix model = new Appendix(); //model.Id = 0; model.FileName = fileName; model.Name = appendix.Name; _context.Add(model); var task = _context.Tasks.Find(id); await _context.SaveChangesAsync(); task.AppendixId = model.Id; _context.Update(task); await _context.SaveChangesAsync(); return(RedirectToAction("TaskForModule", new { id = task.ModuleId })); } return(View(appendix)); }
// GET: Appendixes/Edit/5 public async Task <IActionResult> EditAppendix(int?id) { if (id == null) { return(NotFound()); } var task = _context.Tasks.FirstOrDefault(e => e.AppendixId == id); var appendix = await _context.Appendices.FindAsync(id); AppendixViewModel returnModel = new AppendixViewModel(); returnModel.Id = appendix.Id; returnModel.Name = appendix.Name; ViewBag.Id = task.ModuleId; if (returnModel == null) { return(NotFound()); } return(View(returnModel)); }
public async Task <IActionResult> EditAppendix([Bind("Id,Name,FileName")] AppendixViewModel appendix) { if (ModelState.IsValid) { var model = _context.Appendices.Find(appendix.Id); try { string fileName = ""; if (appendix.FileName != null) { string uploadPath = Path.Combine(hostingEnvironment.WebRootPath, "images"); fileName = Guid.NewGuid().ToString() + "_" + appendix.FileName.FileName; string FilePath = Path.Combine(uploadPath, fileName); appendix.FileName.CopyTo(new FileStream(FilePath, FileMode.Create)); System.IO.File.Delete(uploadPath + model.FileName); } model.FileName = fileName; model.Name = appendix.Name; _context.Update(model); await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!AppendixExists(appendix.Id)) { return(NotFound()); } else { throw; } } var task = _context.Tasks.FirstOrDefault(e => e.AppendixId == model.Id); return(RedirectToAction("TaskForModule", new { id = task.ModuleId })); } return(View(appendix)); }
public async Task <IActionResult> Create([Bind("Id,Name,FileName")] AppendixViewModel appendix) { if (ModelState.IsValid) { string fileName = ""; if (appendix.FileName != null) { string uploadPath = Path.Combine(hostingEnvironment.WebRootPath, "images"); fileName = Guid.NewGuid().ToString() + "_" + appendix.FileName.FileName; string FilePath = Path.Combine(uploadPath, fileName); appendix.FileName.CopyTo(new FileStream(FilePath, FileMode.Create)); } Appendix model = new Appendix(); model.FileName = fileName; model.Name = appendix.Name; _context.Add(model); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(appendix)); }