public async Task <ActionResult> Edit(FEP.Intranet.Areas.eEvent.Models.EditEventSpeakerModel model) { if (model.Attachments.Count() == 0 && model.AttachmentFiles.Count() == 0) { ModelState.AddModelError("Attachments", "Please upload file"); } if (ModelState.IsValid) { var modelapi = new EditEventSpeakerModel() { UserId = model.UserId, UserName = model.UserName, SpeakerType = model.SpeakerType, SpeakerStatus = model.SpeakerStatus, Experience = model.Experience, Attachments = model.Attachments, }; //attachment if (model.AttachmentFiles.Count() > 0) { var responseFile = await FileMethod.UploadFile(model.AttachmentFiles.ToList(), CurrentUser.UserId); if (responseFile != null) { modelapi.FilesId = responseFile.Select(f => f.Id).ToList(); } } var response = await WepApiMethod.SendApiAsync <bool>(HttpVerbs.Put, $"eEvent/EventSpeaker?id={model.Id}", modelapi); if (response.isSuccess) { //LogActivity("Update Event Speaker", model); TempData["SuccessMessage"] = "Event Speaker successfully updated"; return(RedirectToAction("List")); } } model.UserIds = new SelectList(await GetUsers(), "Id", "Name", 0); //TempData["ErrorMessage"] = "Fail to update Event Speaker"; return(View(model)); }
public IHttpActionResult Put(int id, [FromBody] EditEventSpeakerModel model) { var speaker = db.EventSpeaker.Where(u => u.Id == id).FirstOrDefault(); if (speaker == null) { return(NotFound()); } speaker.UserId = model.UserId; speaker.SpeakerType = model.SpeakerType; speaker.Experience = model.Experience; speaker.SpeakerStatus = model.SpeakerStatus; db.EventSpeaker.Attach(speaker); db.Entry(speaker).Property(x => x.UserId).IsModified = true; db.Entry(speaker).Property(x => x.SpeakerType).IsModified = true; db.Entry(speaker).Property(x => x.Experience).IsModified = true; db.Entry(speaker).Property(x => x.SpeakerStatus).IsModified = true; db.Entry(speaker).Property(x => x.Display).IsModified = false; db.Entry(speaker).Property(x => x.Id).IsModified = false; //remove file var attachments = db.EventFile.Where(s => s.FileCategory == EventFileCategory.EventSpeaker && 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 files foreach (var fileid in model.FilesId) { var eventfile = new EventFile { FileCategory = EventFileCategory.EventSpeaker, FileId = fileid, ParentId = speaker.Id }; db.EventFile.Add(eventfile); } db.Configuration.ValidateOnSaveEnabled = true; db.SaveChanges(); return(Ok(true)); }