public IActionResult OnPostAddBook()
        {
            if (ModelState.IsValid)
            {
                var book = new BookLibrary.Models.Book();
                book.Title = this.AddBookBindingModel.Title;

                var author = _context.Authors.FirstOrDefault(a => a.Name == this.AddBookBindingModel.Author);
                if (author is null)
                {
                    author = new BookLibrary.Models.Author {
                        Name = this.AddBookBindingModel.Author
                    };
                    this._context.Authors.Add(author);
                    this._context.SaveChanges();
                }

                book.AuthorId    = author.Id;
                book.ImageUrl    = this.AddBookBindingModel.ImageUrl;
                book.Description = this.AddBookBindingModel.Description;

                this._context.Books.Add(book);
                this._context.SaveChanges();

                return(RedirectToPage("/Index"));
            }

            return(Page());
        }
Ejemplo n.º 2
0
        public IActionResult OnPost()
        {
            var author = new BookLibrary.Models.Author {
                Name = this.Name
            };

            this._context.Authors.Add(author);
            this._context.SaveChanges();

            return(RedirectToPage("/Index"));
        }