// GET: /Admin/Category/Create public ActionResult Create(int? parentCategoryId) { Category category = new Category(); category.CategoryParentId = parentCategoryId; ViewBag.CategoryList = new SelectList(db.Categories.ToList(), "CategoryId", "Name", parentCategoryId); return View(category); }
public ActionResult Create(Category category, HttpPostedFileBase image) { if (ModelState.IsValid) { if (image != null) { string ext = System.IO.Path.GetExtension(image.FileName); string path = System.IO.Path.Combine( Server.MapPath("~/Areas/Admin/Uploads"), System.DateTime.Now.Ticks.ToString() + ext); // file is uploaded category.Image = System.IO.Path.GetFileName(path); image.SaveAs(path); } category.CreatedOn = DateTime.Now; db.Categories.Add(category); db.SaveChanges(); return RedirectToAction("Index", new { id = category.CategoryParentId }); } return View(category); }
public ActionResult Edit(Category category, HttpPostedFileBase image) { if (ModelState.IsValid) { if (image != null) { string tempPath = Server.MapPath("~/Areas/Admin/Uploads/" + category.Image); if (!string.IsNullOrWhiteSpace(category.Image)) System.IO.File.Delete(Server.MapPath("~/Areas/Admin/Uploads/" + category.Image)); string ext = System.IO.Path.GetExtension(image.FileName); string path = System.IO.Path.Combine( Server.MapPath("~/Areas/Admin/Uploads"), System.DateTime.Now.Ticks.ToString() + ext); // file is uploaded category.Image = System.IO.Path.GetFileName(path); image.SaveAs(path); } db.Entry(category).State = System.Data.Entity.EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(category); }