public ActionResult Create(Book book, HttpPostedFileBase imagePath) { if (ModelState.IsValid) { if(imagePath != null && imagePath.ContentLength > 0) { string pic = System.IO.Path.GetFileName(imagePath.FileName); string path = System.IO.Path.Combine(Server.MapPath("~/Content/imageBook"), pic); if (System.IO.File.Exists(path)) { int count = 2; while (System.IO.File.Exists(path)) { pic = count.ToString() + pic; path = System.IO.Path.Combine(Server.MapPath("~/Content/imageBook"), pic); } } imagePath.SaveAs(path); book.imagePath = pic; } db.Books.Add(book); db.SaveChanges(); return RedirectToAction("Index"); } ViewBag.CategoriesID = new SelectList(db.Categories, "ID", "Name"); return View(book); }