コード例 #1
0
        //public ActionResult Edit(int? id)
        //{
        //    if (id == null)
        //    {
        //        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        //    }
        //    Books books = db.Books.Find(id);
        //    if (books == null)
        //    {
        //        return HttpNotFound();
        //    }
        //    var authors = db.Authors.ToList();
        //    var model = authors.Select(a => new AuthorViewModel(a));
        //    ViewBag.AuthorID = new SelectList(model, "AuthorID", "AuthorName");
        //    ViewBag.CategoryID = new SelectList(db.Category, "CategoryID", "CategoryName", books.CategoryID);
        //    return View(books);
        //}


        //// POST: Books/Edit/5
        //// To protect from overposting attacks, please enable the specific properties you want to bind to, for
        //// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        //[HttpPost]
        //[ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include = "BookID,Created,Modified,BookName,Description,Image,AuthorID,CategoryID")] Books books)
        //{
        //    if (ModelState.IsValid)
        //    {
        //        var ticks = DateTime.Now.Ticks;
        //        books.Created = ticks;
        //        var bookFromDb = db.Books.Find(books.BookID);
        //        DateTime date = new DateTime(ticks, DateTimeKind.Local);
        //        bookFromDb.Modified = date.Ticks;
        //        bookFromDb.BookName = bookFromDb.BookName;

        //        db.SaveChanges();
        //        return RedirectToAction("Index");
        //    }
        //    ViewBag.AuthorID = new SelectList(db.Authors, "AuthorID", "FirstName", books.AuthorID);
        //    ViewBag.CategoryID = new SelectList(db.Category, "CategoryID", "CategoryName", books.CategoryID);
        //    return View(books);
        //}



        // GET: Books/Edit/5
        public ActionResult Edit(int id)
        {
            var book = db.Books.Find(id);

            var authors    = db.Authors.ToList();
            var categories = db.Category.ToList();

            var model = new ViewModels.BookViewModel.Edit(book, categories, authors);

            return(View(model));
        }
コード例 #2
0
        public ActionResult Edit(ViewModels.BookViewModel.Edit model)
        {
            var book = new Books();

            if (ModelState.IsValid)
            {
                book.Created     = DateTime.Now.Ticks;
                book.BookName    = model.Name;
                book.Description = model.Discription;
                book.AuthorID    = model.SelectedAuthorId;
                book.CategoryID  = model.SelectedCategoryId;

                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(model));
        }