コード例 #1
0
ファイル: BooksController.cs プロジェクト: TGrigor/Book-Store
        public async Task <ActionResult> Edit([Bind(Include = "BookID,Title,AuthorID,CountryID,Price,Description,PagesCount,Picture")] Book book, HttpPostedFileBase image1)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    if (image1 != null)
                    {
                        FileInfo file     = new FileInfo(image1.FileName);
                        var      fileName = Path.GetFileName(image1.FileName);
                        string   GuIdName = Guid.NewGuid().ToString() + file.Extension;
                        var      path     = Path.Combine(Server.MapPath("~/Picture/"), GuIdName);
                        book.Picture = GuIdName;
                        image1.SaveAs(path);
                    }
                    else
                    {
                        using (BookStoreDatabaseEntities db1 = new BookStoreDatabaseEntities())
                        {
                            var img = db1.Books.Find(book.BookID).Picture;
                            book.Picture = img;
                        }
                    }
                    //if (image1!=null)
                    //{
                    //    book.Picture = new byte[image1.ContentLength];
                    //    image1.InputStream.Read(book.Picture, 0, image1.ContentLength);

                    //}
                    //if (image1==null)
                    //{
                    //    using (BookStoreDatabaseEntities db1 = new BookStoreDatabaseEntities())
                    //    {
                    //        var img = db1.Books.Find(book.BookID).Picture;
                    //        book.Picture = img;
                    //    }
                    //}
                    db.Entry(book).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                ViewBag.AuthorID  = new SelectList(db.Authors, "AuthorID", "FullName", book.AuthorID);
                ViewBag.CountryID = new SelectList(db.Countries, "CountryID", "CountryName", book.CountryID);
                return(View(book));
            }
            catch
            {
                return(PartialView("Error404"));
            }
        }
コード例 #2
0
        public async Task <ActionResult> Edit([Bind(Include = "AttributeID,Name,AttributeTypeID")] ExtraAttribute extraAttribute)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(extraAttribute).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                ViewBag.AttributeTypeID = new SelectList(db.AttributeTypes, "AttributeTypeID", "Name", extraAttribute.AttributeTypeID);
                return(View(extraAttribute));
            }
            catch
            {
                return(PartialView("Error404"));
            }
        }
コード例 #3
0
        public async Task <ActionResult> Edit([Bind(Include = "BookAddAttributesID,BookID,AttributeID,ValueTypeText,ValueTypeDate,Ganre,GanreID")] BookAddAttribute bookAddAttribute)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Entry(bookAddAttribute).State = EntityState.Modified;
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                ViewBag.BookID      = new SelectList(db.Books, "BookID", "Title", bookAddAttribute.BookID);
                ViewBag.AttributeID = new SelectList(db.ExtraAttributes, "AttributeID", "Name", bookAddAttribute.AttributeID);
                ViewBag.GanreID     = new SelectList(db.Ganres, "GanreID", "GanreName", bookAddAttribute.GanreID);
                return(View(bookAddAttribute));
            }
            catch
            {
                return(PartialView("Error404"));
            }
        }
コード例 #4
0
        public async Task <ActionResult> Edit([Bind(Include = "AuthorID,AuthorName,AuthorLastName,BirthDay,Picture")] Author author, HttpPostedFileBase image1)
        {
            if (ModelState.IsValid)
            {
                if (image1 != null)
                {
                    author.Picture = new byte[image1.ContentLength];
                    image1.InputStream.Read(author.Picture, 0, image1.ContentLength);
                }
                if (image1 == null)
                {
                    using (BookStoreDatabaseEntities db1 = new BookStoreDatabaseEntities())
                    {
                        var img = db1.Authors.Find(author.AuthorID).Picture;
                        author.Picture = img;
                    }
                }
                db.Entry(author).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(author));
        }