public async Task <ActionResult <SubjectViewModel> > Create(SubjectViewModel subjectView) { try { if (ModelState.IsValid) { //var subject = _mapper.Map<Subject>(subjectView); var subject = _mapper.Map <SubjectViewModel, Subject>(subjectView); var result = await _context.Subjects.AddAsync(subject); var save = await _context.SaveChangesAsync(); // var subject = _mapper.Map<SubjectViewModel, Subject>(subjectView); //var save = _manager.Create(subject); return(RedirectToAction("Index")); //if (save != null) //{ // return RedirectToAction("Index"); //} } return(View()); } catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task <ActionResult> AddBook(BookViewModel book) { try { //var bookAuthors = new List<BookAuthor>(); //var bookSubjects = new List<BookSubject>(); if (ModelState.IsValid) { //Author bookAuthor = _context.Authors.SingleOrDefault(c => c.AuthorId == book.AuthorId); //Subject bookSubject = _context.Subjects.Single(c => c.SubjectId == book.SubjectId); Book newBook = new Book() { BookId = book.BookId, //BookSubjects = bookSubject, //BookAuthors = bookAuthor, CallNumber = book.CallNumber, Description = book.Description, ISBN = book.ISBN, MaxIssueDays = book.MaxIssueDays, Language = book.Language, Publisher = book.Publisher, Title = book.Title, Issue = book.Issue }; var result = await _context.Books.AddAsync(newBook); await _context.SaveChangesAsync(); } //PopulateSubjectsDropDownList(book.BookSubjects); //PopulateAuthorsDropDownList(book.BookAuthors); //var bookAuthors = new List<BookAuthor>(); //var bookSubjects = new List<BookSubject>(); return(RedirectToAction("GetBooks")); } catch (Exception ex) { throw new Exception(ex.Message); } }
public async Task Create(Subject subject) { try { if (subject != null) { //var newResult = new Subject() //{ // SubjectId = subject.SubjectId, // Name = subject.Name, // BookSubjects = subject.BookSubjects //}; var result = await _context.Subjects.AddAsync(subject); var save = await _context.SaveChangesAsync(); } } catch (Exception ex) { throw new Exception(ex.Message); } }