public RequestResult EditLiterature(LiteratureViewModel lit) { foreach (var book in lit.Books.Where(book => DataProvider.LibraryBooks.Filter(p=>p.BookNumber==book.BookNumber && p.Id!=book.RowId).Any())) { return new RequestResult(1, "Книга с номером - " + book.BookNumber + "- уже существует"); } var literature = DataProvider.LibraryLiteratures.FirstOrDefault(p => p.Id == lit.Id); literature = literature ?? new Library_Literature(); literature.FullName = lit.FullName; literature.ShortName = lit.ShortName; literature.Year = lit.Year; literature.PublishingHouse = lit.PublishingHouse; literature.MainAuthor = lit.MainAuthor; literature.AdditionalAuthors = lit.AdditionalAuthors; literature.Desqription = lit.Desqription; try { if (literature.Id==0) DataProvider.LibraryLiteratures.Add(literature); DataProvider.Save(); foreach (var book in lit.Books) { var editBook = literature.Library_Book.FirstOrDefault(p => p.Id == book.RowId); if (editBook == null) { DataProvider.LibraryBooks.Add(new Library_Book() { BookNumber = book.BookNumber, Library_Literature = literature }); } else editBook.BookNumber = book.BookNumber; DataProvider.Save(); } return RequestResult.Ok; } catch (Exception) { return new RequestResult(1,"ЗАполните все данные формы"); } }
public ActionResult Save(LiteratureViewModel model) { var updateResult = _literatureService.EditLiterature(model); return Json(updateResult.Equals(RequestResult.Ok) ? new { type = "success", message = "Литература успешно сохранена" } : new { type = "error", message = updateResult.RequestMessage }); }