示例#1
0
        public async Task <ActionResult> Create([Bind(Include = "Id, AuthorName")] Author author)
        {
            if (ModelState.IsValid)
            {
                db.Authors.Add(author);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(author));
        }
        public async Task <ActionResult> Create([Bind(Include = "Id,GenreName")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                db.Genres.Add(genre);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            return(View(genre));
        }
示例#3
0
        public async Task <ActionResult> Create([Bind(Include = "Id, CommentBody,UserName,Email,Rating,BookId")] Comment comment)
        {
            if (ModelState.IsValid)
            {
                comment.CommentTime = DateTime.Now;
                db.Comments.Add(comment);
                await db.SaveChangesAsync();

                return(RedirectToAction("Index"));
            }

            ViewBag.BookId = new SelectList(db.Books, "Id", "Name", comment.BookId);
            return(View(comment));
        }
        public async Task <ActionResult> Create([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;
                    }
                }
                db.Books.Add(book);
                await db.SaveChangesAsync();

                Directory.CreateDirectory(Server.MapPath("~/App_Data/warehouse/book_" + book.Id));

                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();
                    }
                }
                return(RedirectToAction("Index"));
            }
            return(View(book));
        }