public ActionResult Create(Support support) { if (ModelState.IsValid) { List<FileDetail> fileDetails = new List<FileDetail>(); for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); FileDetail fileDetail = new FileDetail() { FileName = fileName, Extension = Path.GetExtension(fileName), Id = Guid.NewGuid() }; fileDetails.Add(fileDetail); var path = Path.Combine(Server.MapPath("~/App_Data/Upload/"), fileDetail.Id + fileDetail.Extension); file.SaveAs(path); } } support.FileDetails = fileDetails; db.Supports.Add(support); db.SaveChanges(); return RedirectToAction("Index"); } return View(support); }
public ActionResult Edit(Support support) { if (ModelState.IsValid) { //New Files for (int i = 0; i < Request.Files.Count; i++) { var file = Request.Files[i]; if (file != null && file.ContentLength > 0) { var fileName = Path.GetFileName(file.FileName); FileDetail fileDetail = new FileDetail() { FileName = fileName, Extension = Path.GetExtension(fileName), Id = Guid.NewGuid(), SupportId = support.SupportId }; var path = Path.Combine(Server.MapPath("~/App_Data/Upload/"), fileDetail.Id + fileDetail.Extension); file.SaveAs(path); db.Entry(fileDetail).State = EntityState.Added; } } db.Entry(support).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(support); }