public ActionResult Create(EditorBookViewModel model) { if (!ModelState.IsValid) { model.CategoriaOptions = new SelectList(_categorias, "Id", "Nome"); return(View(model)); } var livro = new Livro { Nome = model.Nome, ISBN = model.ISBN, DataLancamento = model.DataLancamento, CategoriaId = model.CategoriaId }; _db.Livros.Add(livro); try { _db.SaveChanges(); } catch (Exception ex) { ModelState.AddModelError("Mensagem", ex.Message); model.CategoriaOptions = new SelectList(_categorias, "Id", "Nome"); return(View(model)); } return(RedirectToAction("Index")); }
public ActionResult Create(EditorBookViewModel model) { if (!ModelState.IsValid) { var categorias = _db.Categorias.ToList(); model.CategoriaOptions = new SelectList(categorias, "Id", "Nome"); return(View(model)); } var livro = new Livro(); livro.Nome = model.Nome; livro.ISBN = model.ISBN; livro.DataLancamento = model.DataLancamento; livro.CategoriaId = model.CategoriaId; _db.Livros.Add(livro); try { throw new Exception("Falha no banco"); _db.SaveChanges(); } catch (Exception ex) { ModelState.AddModelError("Mensagem", ex.Message); var categorias = _db.Categorias.ToList(); model.CategoriaOptions = new SelectList(categorias, "Id", "Nome"); return(View(model)); } return(RedirectToAction("Index")); }
public IActionResult Create(Role item) { if (ModelState.IsValid) { db.Add(item); db.SaveChanges(); return(RedirectToAction("Index")); } return(View()); }
public bool Create(Livro livro) { try { _db.Livros.Add(livro); _db.SaveChanges(); return(true); } catch { return(false); } }
public bool Inserir(Autor autor) { try { _context.Autores.Add(autor); _context.SaveChanges(); } catch { return(false); } return(true); }
public bool Create(Book book) { try { _db.Books.Add(book); _db.SaveChanges(); return(true); } catch { return(false); } }
public bool Create(Author author) { try { _db.Authors.Add(author); _db.SaveChanges(); return(true); } catch { return(false); } }
public ActionResult Create(EditorBookViewModel model) { var livro = new Livro(); livro.Nome = model.Nome; livro.ISBN = model.ISBN; livro.DataLancamento = model.DataLancamento; livro.CategoriaId = model.CategoriaId; _db.Livros.Add(livro); _db.SaveChanges(); return(RedirectToAction("Index")); }
public bool Create(Category category) { try { _db.Categories.Add(category); _db.SaveChanges(); return(true); } catch { return(false); } }
public bool Create(Book book) { try { _db.Livros.Add(book); _db.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public bool Create(Author autor) { try { _db.Autores.Add(autor); _db.SaveChanges(); return(true); } catch (Exception) { return(false); } }
public void Post([FromBody] Author item) { BookStoreDataContext db = new BookStoreDataContext(); db.Add(item); db.SaveChanges(); }
public void Delete(int id) { BookStoreDataContext db = new BookStoreDataContext(); var item = db.Authors.Find(id); db.Authors.Remove(item); db.SaveChanges(); }
public bool Create(Autor autor) { try { _db.Entry <Autor>(autor).State = EntityState.Detached; _db.Aturores.Attach(autor); _db.Aturores.Add(autor); _db.SaveChanges(); return(true); } catch (Exception e) { Console.WriteLine(e.Message); return(false); } }
public IActionResult Create(BookFormViewModel item) { ViewBag.Categories = new SelectList(db.Categories.ToList(), "CategoryID", "Name"); ViewBag.Authors = new MultiSelectList(db.Authors.ToList(), "AuthorID", "Name"); if (ModelState.IsValid) { Book book = new Book(); book.ISBN = item.ISBN; book.CategoryID = item.CategoryID; book.Title = item.Title; book.PublishDate = item.PublishDate; book.Price = item.Price; book.Quantity = item.Quantity; db.Add(book); foreach (int authorId in item.AuthorIDs) { BookAuthor bookAuthor = new BookAuthor(); bookAuthor.ISBN = item.ISBN; bookAuthor.AuthorID = authorId; db.Add(bookAuthor); } db.SaveChanges(); if (item.Photo != null) { var file = item.Photo; var uploads = Path.Combine(_environment.WebRootPath, "upload"); if (file.Length > 0) { using (var fileStream = new FileStream(Path.Combine(uploads, item.ISBN + ".jpg"), FileMode.Create)){ file.CopyToAsync(fileStream); } } } return(RedirectToAction("Index")); } return(View()); }
public ActionResult Create(EditorBookViewModel model) { try { var livro = new Livro(); livro.Nome = model.Nome; livro.ISBN = model.ISBN; livro.DataLancamento = model.DataLancamento; livro.CategoriaId = model.CategoriaId; _db.Livros.Add(livro); _db.SaveChanges(); return(RedirectToAction("Index")); } catch (Exception e) { Console.WriteLine("Error", e.Message); } return(View()); }
public IActionResult Create(UserCreateFormViewModel item) { ViewBag.Roles = new SelectList(db.Roles.ToList(), "RoleID", "RoleName"); if (ModelState.IsValid) { User user = new User(); user.UserName = item.UserName; user.RoleID = item.RoleID; user.Email = item.Email; user.Password = EncryptString(item.Password, key); user.Fullname = item.Fullname; db.Add(user); db.SaveChanges(); return(RedirectToAction("Index")); } return(View()); }
public void Create(Book entity) { _db.Books.Add(entity); _db.SaveChanges(); }
public void Create(Author entity) { _db._Authors.Add(entity); _db.SaveChanges(); }