public async Task <ActionResult> Edit([Bind(Include = "Id, Name, Year, AuthorId, GenreId, Cover, CoverType, Description")] Book book, HttpPostedFileBase file, IEnumerable <HttpPostedFileBase> files)
        {
            if (ModelState.IsValid)
            {
                if (file != null)
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        file.InputStream.CopyTo(ms);
                        byte[] array = ms.GetBuffer();
                        book.Cover     = array;
                        book.CoverType = file.ContentType;
                    }
                }

                foreach (var f in files)
                {
                    if (f != null && f.ContentLength > 0)
                    {
                        var fileName = Path.GetFileName(f.FileName);
                        var path     = Path.Combine(Server.MapPath("~/App_Data/warehouse/book_" + book.Id), fileName);
                        f.SaveAs(path);

                        FilePath fp = new FilePath()
                        {
                            BookID   = book.Id,
                            FileName = Globals.resolveVirtual(path)
                        };
                        db.FilePaths.Add(fp);
                        db.SaveChanges();
                    }
                }
                db.Entry(book).State = EntityState.Modified;
                if (file == null)
                {
                    db.Entry(book).Property("CoverType").IsModified = false;

                    db.Entry(book).Property("Cover").IsModified = false;
                }

                await db.SaveChangesAsync();

                return(Redirect(Request.UrlReferrer.ToString()));
            }
            return(View(book));
        }
예제 #2
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,Name")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Entry(author).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            return(View(author));
        }
예제 #3
0
        public async Task <ActionResult> Edit([Bind(Include = "Id,CommentBody,UserName,Email,Date,Rating,BookId")] Comment comment)
        {
            if (ModelState.IsValid)
            {
                db.Entry(comment).State = EntityState.Modified;
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }
            ViewBag.BookId = new SelectList(db.Books, "Id", "Name", comment.BookId);
            return(View(comment));
        }