public async Task <ActionResult> Edit(FEP.Intranet.Areas.eEvent.Models.EditEventAgendaModel model) { if (model.Attachments.Count() == 0 && model.AttachmentFiles.Count() == 0) { ModelState.AddModelError("Attachments", "Please upload file"); } if (ModelState.IsValid) { var modelapi = new EditEventAgendaModel { Id = model.Id, AgendaTitle = model.AgendaTitle, AgendaDescription = model.AgendaDescription, Time = model.Time, Tentative = model.Tentative, EventId = model.EventId, PersonInChargeId = model.PersonInChargeId, Attachments = model.Attachments, }; //attachment if (model.AttachmentFiles.Count() > 0) { var responseFile = await WepApiMethod.SendApiAsync <List <FileDocument> >($"File?userId={CurrentUser.UserId}", model.AttachmentFiles.ToList()); if (responseFile.isSuccess) { modelapi.FilesId = responseFile.Data.Select(f => f.Id).ToList(); } } var response = await WepApiMethod.SendApiAsync <bool>(HttpVerbs.Put, $"eEvent/EventAgenda?id={model.Id}", modelapi); if (response.isSuccess) { await LogActivity(Modules.Event, "Edit Event Agenda", model); TempData["SuccessMessage"] = "Event Agenda successfully updated"; return(RedirectToAction("List")); } } model.EventIds = new SelectList(await GetEvent(), "Id", "RefNo"); model.PersonInchargeIds = new SelectList(await GetUsers(), "Id", "Name"); return(View(model)); }
//Edit public IHttpActionResult Put(int id, [FromBody] EditEventAgendaModel model) { var agenda = db.EventAgenda.Where(u => u.Id == id).FirstOrDefault(); if (agenda == null) { return(NotFound()); } agenda.AgendaTitle = model.AgendaTitle; agenda.AgendaDescription = model.AgendaDescription; agenda.Tentative = model.Tentative; agenda.Time = model.Time; agenda.EventId = model.EventId; agenda.PersonInChargeId = model.PersonInChargeId; db.Entry(agenda).State = EntityState.Modified; db.Entry(agenda).Property(x => x.Display).IsModified = false; //remove file var attachments = db.EventFile.Where(s => s.FileCategory == EventFileCategory.EventAgenda && s.ParentId == model.Id).ToList(); if (attachments != null) { //delete all if (model.Attachments == null) { foreach (var attachment in attachments) { attachment.FileDocument.Display = false; db.FileDocument.Attach(attachment.FileDocument); db.Entry(attachment.FileDocument).Property(m => m.Display).IsModified = true; db.EventFile.Remove(attachment); } } else { foreach (var attachment in attachments) { if (!model.Attachments.Any(u => u.Id == attachment.FileDocument.Id)) //delete if not exist anymore { attachment.FileDocument.Display = false; db.FileDocument.Attach(attachment.FileDocument); db.Entry(attachment.FileDocument).Property(m => m.Display).IsModified = true; db.EventFile.Remove(attachment); } } } } //add new file //files foreach (var fileid in model.FilesId) { var eventfile = new EventFile { FileCategory = EventFileCategory.EventAgenda, FileId = fileid, ParentId = agenda.Id }; db.EventFile.Add(eventfile); } db.Configuration.ValidateOnSaveEnabled = true; db.SaveChanges(); return(Ok(true)); }