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();
}