public ActionResult Edit(Audiofile model) { try { if (ModelState.IsValid) { EditAudiofile(model); return RedirectToAction("Audiofiles", "Admin"); } } catch (DataException) { ModelState.AddModelError("", "Не удалось сохранить изменения. Повторите попытку."); } ViewBag.AuthorID = new SelectList(unitOfWork.AuthorRepository.Get(orderBy: x => x.OrderBy(y => y.Name)), "ID", "Name"); ViewBag.GenreID = new SelectList(unitOfWork.GenreRepository.Get(orderBy: x => x.OrderBy(y => y.Name)), "ID", "Name"); return View(model); }
private void EditAudiofile(Audiofile model) { var audiofileToUpdate = unitOfWork.AudiofileRepository.GetByID(model.ID); audiofileToUpdate.Title = model.Title; audiofileToUpdate.Genre = unitOfWork.GenreRepository.GetByID(model.GenreID); audiofileToUpdate.Author = unitOfWork.AuthorRepository.GetByID(model.AuthorID); audiofileToUpdate.Description = model.Description; audiofileToUpdate.Year = model.Year; unitOfWork.AudiofileRepository.Update(audiofileToUpdate); unitOfWork.Save(); }
private void AddAudiofile(AudiofileUploadViewModel model) { Audiofile audiofile = new Audiofile { Title = model.Name, AuthorID = model.AuthorID, GenreID = model.GenreID, Year = model.Year, Description = model.Description, Filename = FileService.SaveFile(model.File) }; bool exist = unitOfWork.AudiofileRepository.Get().FirstOrDefault( x => x.Title.ToUpper() == audiofile.Title.ToUpper() && x.AuthorID == audiofile.AuthorID && x.GenreID == audiofile.GenreID && x.Year == audiofile.Year && x.Description.ToUpper() == audiofile.Description.ToUpper() ) != null; if (exist) { throw new Exception(); } unitOfWork.AudiofileRepository.Insert(audiofile); unitOfWork.Save(); }