Exemplo n.º 1
0
        public ActionResult Create(Book book, HttpPostedFileBase PathImage)
        {
            if(ModelState.IsValid)
            {
                if ((PathImage != null && PathImage.ContentLength > 0))
                {
                    var fileName = Path.GetFileName(PathImage.FileName);
                    //var imageName = "book" + db.Books.Select(b=>b.Id).Max() + ".jpg";
                    var path = Path.Combine(Server.MapPath("~/Images"), fileName);
                    PathImage.SaveAs(path);

                    book.PathImage = "~/Images/" + fileName;
                }
                else
                {
                    book.PathImage = "~/Images/default.jpg";
                }

                book.Category = db.Categories.Where(c => c.Name == book.Category.Name).ToList()[0];
                db.Books.Add(book);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Categories = db.Categories.ToList();
            return View();
        }
Exemplo n.º 2
0
        public ActionResult Edit(Book book, HttpPostedFileBase PathImage)
        {
            if (ModelState.IsValid)
            {
                if ((PathImage != null && PathImage.ContentLength > 0))
                {
                    var fileName = Path.GetFileName(PathImage.FileName);
                    var imageName = "book" + db.Books.Select(b => b.Id).Max() + ".jpg";
                    var path = Path.Combine(Server.MapPath("~/Images"), imageName);
                    PathImage.SaveAs(path);

                    book.PathImage = "/Images/" + imageName;
                }

                db.Entry(book).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.Categories = db.Categories.ToList();
            return View(book);
        }