public ActionResult Create(CreateEditEventVM eventVM, HttpPostedFileBase[] upload) { try { if (eventVM.IdSelectedThema.HasValue) { eventVM.Event.Thema = repoThema.GetById((int)eventVM.IdSelectedThema.Value); } List <Picture> pictures = new List <Picture>(); foreach (HttpPostedFileBase file in upload) { var extension = file.FileName.Split('.').Last <string>(); var InputFileName = Guid.NewGuid().ToString() + "." + extension; var ServerSavePath = Path.Combine(Server.MapPath("~/Content/media/img/") + InputFileName); file.SaveAs(ServerSavePath); var picture = new Picture(); picture.FileName = InputFileName; pictures.Add(picture); } eventVM.Event.Pictures = pictures; repoEvent.Insert(eventVM.Event); return(RedirectToAction("Index")); } catch (Exception e) { eventVM.Themas = repoThema.GetAll(); return(View(eventVM)); } }
public ActionResult Create() { var vm = new CreateEditEventVM(); vm.Themas = repoThema.GetAll(); return(View(vm)); }
public ActionResult Edit(CreateEditEventVM vm, HttpPostedFileBase[] upload, string[] pictureIds) { try { Event ev = (Event)vm.Event.Clone(); ev.Pictures = new List <Picture>(); if (vm.IdSelectedThema.HasValue) { ev.Thema = repoThema.GetById(vm.IdSelectedThema.Value); } // Delete pictures. var prevPictures = repoEvent.GetSet().Where(e => e.Id == vm.Event.Id).Include(e => e.Pictures).First().Pictures; foreach (Picture p in prevPictures) { if (pictureIds.Contains(p.Id.ToString())) { ev.Pictures.Add(p); } else { System.IO.File.Delete(Server.MapPath("~/Content/media/img/") + p.FileName); } } // Add new pictures. foreach (HttpPostedFileBase file in upload) { if (file != null) { var extension = file.FileName.Split('.').Last <string>(); var InputFileName = Guid.NewGuid() + "." + extension; var ServerSavePath = Path.Combine(Server.MapPath("~/Content/media/img/") + InputFileName); file.SaveAs(ServerSavePath); var picture = new Picture(); picture.FileName = InputFileName; ev.Pictures.Add(picture); } } repoEvent.Update(ev); return(RedirectToAction("Index")); } catch { vm.Themas = repoThema.GetAll(); return(View(vm)); } }
public ActionResult Edit(int id) { var vm = new CreateEditEventVM(); vm.Event = repoEvent.GetById(id); if (vm.Event.Thema != null) { vm.IdSelectedThema = vm.Event.Thema.Id; } vm.Themas = repoThema.GetAll(); return(View(vm)); }