Exemplo n.º 1
0
        public ActionResult Create([Bind(Include = "Id,FirstName,LastName,Biography")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Authors.Add(author);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(author));
        }
Exemplo n.º 2
0
 public ActionResult Edit([Bind(Include = "ISBN,Title,PublishedYear,Price,Count,AuthorId")] Book book)
 {
     if (ModelState.IsValid)
     {
         db.Entry(book).State = EntityState.Modified;
         db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     ViewBag.AuthorId = new SelectList(db.Authors, "Id", "FirstName", book.AuthorId);
     return(View(book));
 }
        public ActionResult DeleteConfirmed(int id)
        {
            Book book = db.Books.Find(id);

            db.Books.Remove(book);
            db.SaveChanges();
            return(RedirectToAction("Index"));
        }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            using (LibraryModel context = new LibraryModel())
            {
                Book book = new Book();
                book.Title  = "Анна Каренина";
                book.Author = "Лев Толстой";

                context.Books.Add(book);
                context.SaveChanges();
            }
        }
        public IHttpActionResult PutBooks(int id, Books books)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != books.Id)
            {
                return(BadRequest());
            }

            var dbBook = db.Books.Find(books.Id);

            dbBook.Title     = books.Title;
            dbBook.Published = books.Published;
            dbBook.Language  = books.Language;
            dbBook.Genre     = books.Genre;

            db.Entry(dbBook).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!BooksExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
        public IHttpActionResult PutAuthors(int id, Authors authors)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != authors.Id)
            {
                return(BadRequest());
            }

            var dbAuthor = db.Authors.Find(authors.Id);

            dbAuthor.FirstName = authors.FirstName;
            dbAuthor.LastName  = authors.LastName;
            dbAuthor.Country   = authors.Country;

            db.Entry(dbAuthor).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AuthorsExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(StatusCode(HttpStatusCode.NoContent));
        }
Exemplo n.º 7
0
 public void AddUser(User user)
 {
     _ctx.Users.Add(user);
     _ctx.SaveChanges();
 }