Ejemplo n.º 1
0
        public async Task <IActionResult> Create([Bind("id,name,genreId,authors")] Book book)
        {
            if (ModelState.IsValid)
            {
                if (book.authorBookList == null)
                {
                    book.authorBookList = new List <AuthorBook>( );
                }
                foreach (int authorId in book.authors)
                {
                    book.authorBookList.Add(
                        new AuthorBook( )
                    {
                        authorId = authorId
                    }
                        );
                }
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            ViewData["genreId"] = new SelectList(_context.genres, "id", "name", book.genreId);
            ViewData["authors"] = new MultiSelectList(_context.authors, "id", "fullName", book.authors);
            return(View(book));
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> Create([Bind("id,name")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                _context.Add(genre);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(genre));
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("id,firstName,lastName,country")] Author author)
        {
            if (ModelState.IsValid)
            {
                if (!this.DoesExist(author.firstName, author.lastName))
                {
                    _context.Add(author);
                    await _context.SaveChangesAsync();

                    return(RedirectToAction(nameof(Index)));
                }
                else
                {
                    ModelState.AddModelError("", "Author already exists!");
                }
            }
            return(View(author));
        }