public void AddBook(Book book, string authorFullName) { var author = booksDbContext.Add <Author>(new Author() { FullName = authorFullName }); booksDbContext.SaveChanges(); book.AuthorId = author.Entity.Id; booksDbContext.Add <Book>(book); }
public void AddBook(Book booktoAdd) { if (booktoAdd == null) { throw new ArgumentNullException(nameof(booktoAdd)); } _context.Add(booktoAdd); }
public async Task <IActionResult> Create([Bind("Id,Title,Author,ISBN,LoanedTo")] Book book) { if (ModelState.IsValid) { _context.Add(book); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(book)); }
public async Task <IActionResult> Create([Bind("Id,タイトル,著者,出版社,発行日,分野,リンク")] Books books) { if (ModelState.IsValid) { _context.Add(books); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(books)); }
public async Task <IActionResult> Create([Bind("Id,Name")] User user) { if (ModelState.IsValid) { _context.Add(user); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(user)); }
public async Task <IActionResult> Create([Bind("BookId,Title,AuthorId")] Book book) { if (ModelState.IsValid) { _context.Add(book); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } ViewData["AuthorId"] = new SelectList(_context.Author, "AuthorId", "Name", book.AuthorId); return(View(book)); }
public async Task <IActionResult> Create([Bind("AuthorId,MyProperty")] Author author) { if (ModelState.IsValid) { _context.Add(author); await _context.SaveChangesAsync(); ViewBag.Title = "Author added successfully"; ViewBag.Message = "New author created successfully!"; return(View("Success")); } return(View(author)); }
[ValidateAntiForgeryToken] //validacao de seguranca public async Task <IActionResult> Create([Bind("AuthorId,Name")] Author author) //serve para evitar alguns ataques, só recebo os campos que estão no bind { if (ModelState.IsValid) { //todo: validacoes adicionais antes de inserir o autor _context.Add(author); await _context.SaveChangesAsync(); //todo: informar o user, autor criado com sucesso return(RedirectToAction(nameof(Index))); } return(View(author)); }
public async Task Post([FromBody] Book book) { _dataContext.Add(book); await _dataContext.SaveChangesAsync(); }