示例#1
0
        public async Task <IActionResult> Create(EditBookViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var book = new Book
            {
                Title       = model.Title,
                BookAuthors =
                    new List <BookAuthor>(model.SelectedAuthors.Select(a => new BookAuthor
                {
                    AuthorId = a, BookId = model.Id
                })),
                Genres = new List <GenreEntry>(model.SelectedGenres.Select(g => new GenreEntry
                {
                    GenreName = g, BookId = model.Id
                }))
            };

            _context.Add(book);
            await _context.SaveChangesAsync();

            return(RedirectToAction(nameof(Index)));
        }
示例#2
0
        public async Task <IActionResult> Create([Bind("Name")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                _context.Add(genre);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(genre));
        }
        public async Task <IActionResult> Create([Bind("Id,Name,Age,Phone")] Borrower borrower)
        {
            if (ModelState.IsValid)
            {
                _context.Add(borrower);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(borrower));
        }
示例#4
0
        public async Task <IActionResult> Create([Bind("Id,FirstName,LastName")] Author author)
        {
            if (ModelState.IsValid)
            {
                _context.Add(author);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
        public async Task <IActionResult> Create([Bind("BookId,Name,Author,Year")] Book book)
        {
            if (ModelState.IsValid)
            {
                _context.Add(book);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(book));
        }
示例#6
0
 public async Task CreateAsync(T entity)
 {
     _dbContext.Set <T>().Add(entity);
     await _dbContext.SaveChangesAsync();
 }