public SermonMetaDataEditViewModel ToEditViewModel() { SermonMetaDataEditViewModel CVM = new SermonMetaDataEditViewModel(); CVM.SermonMetaData = this.SermonMetaData; CVM.SermonAudioFile = this.SermonAudioFile; CVM.SermonCategoryies = this.SermonCategoryies; return CVM; }
public ActionResult Edit(SermonMetaDataEditViewModel sermonMetaDataEditViewModel) { if (ModelState.IsValid) { unitOfWork.context.SermonMetaDatas.Attach(sermonMetaDataEditViewModel.SermonMetaData); unitOfWork.context.Entry(sermonMetaDataEditViewModel.SermonMetaData).Collection(x => x.SermonCategories).Load(); if (sermonMetaDataEditViewModel.SermonAudioFile != null && sermonMetaDataEditViewModel.SermonAudioFile.ContentLength > 0) { SermonData newSermonData = new SermonData(); newSermonData.SermonDataID = Guid.NewGuid(); newSermonData.Active = true; sermonMetaDataEditViewModel.SermonMetaData.SermonDatas.Add(newSermonData); var path = Path.Combine(unitOfWork.SermonConfigRepository.SermonDataPath, Path.GetFileName(newSermonData.SermonDataID.ToString("D").ToUpperInvariant() + Path.GetExtension(sermonMetaDataEditViewModel.SermonAudioFile.FileName))); sermonMetaDataEditViewModel.SermonAudioFile.SaveAs(path); } int[] selectedCategories = sermonMetaDataEditViewModel.SermonCategoryies.Where(x => x.Selected).Select<SelectListItem, int>(x => Convert.ToInt32(x.Value.ToString())).ToArray<int>(); sermonMetaDataEditViewModel.SermonMetaData.SermonCategories.Clear(); sermonMetaDataEditViewModel.SermonMetaData.SermonCategories = unitOfWork.SermonCategoryRepository.Get(x => selectedCategories.Contains(x.SermonCategoryID)).ToList(); unitOfWork.SermonMetaDataRepository.Update(sermonMetaDataEditViewModel.SermonMetaData); unitOfWork.Save(); return RedirectToAction("Index"); } //sermonMetaDataEditViewModel.SermonMetaData = SermonMetaDataSetupBase(sermonMetaDataEditViewModel.SermonMetaData); sermonMetaDataEditViewModel = SermonMetaDataBaseInitialize(sermonMetaDataEditViewModel.SermonMetaData).ToEditViewModel(); return View(sermonMetaDataEditViewModel); }