public ActionResult Create(Categories Categories, HttpPostedFileBase icon) { if (ModelState.IsValid) { Categories.IsActive = true; Categories.CreateDate = DateTime.Now; Categories.CreateUser = User.Identity.Name; string filesPath = "", full_path = ""; if (icon != null) { char DirSeparator = System.IO.Path.DirectorySeparatorChar; filesPath = ConfigurationManager.AppSettings["CategoryMarkerIconLocaion"]; full_path = Server.MapPath(filesPath).Replace("Brands", "").Replace("Admin", ""); Categories.MarkerIcon = FileUpload.UploadFile(icon, full_path); } db.Categories.Add(Categories); db.SaveChanges(); if (icon != null) { string filename = Categories.CategoryID + "_" + icon.FileName.Replace(" ", "_").Replace("-", "_"); Categories.MarkerIcon = FileUpload.UploadFile(icon, filename, full_path); db.Entry(Categories).State = EntityState.Modified; db.SaveChanges(); } return RedirectToAction("Index"); } return View(Categories); }
public ActionResult Edit(Categories Categories, HttpPostedFileBase icon) { try { if (ModelState.IsValid) { Categories.ModifyDate = DateTime.Now; Categories.ModifyUser = User.Identity.Name; if (Categories.CreateUser == null || Categories.CreateUser == "") Categories.CreateUser = "******"; string filesPath = "", full_path = ""; string marker = Categories.MarkerIcon; if (icon != null) { char DirSeparator = System.IO.Path.DirectorySeparatorChar; filesPath = ConfigurationManager.AppSettings["CategoryMarkerIconLocaion"]; full_path = Server.MapPath(filesPath).Replace("Brands", "").Replace("Admin", ""); Categories.MarkerIcon = FileUpload.UploadFile(icon, full_path); } db.Entry(Categories).State = EntityState.Modified; db.SaveChanges(); if (marker + "" != "") FileUpload.DeleteFile(marker, full_path); if (icon != null) { string filename = Categories.CategoryID + "_" + icon.FileName.Replace(" ", "_").Replace("-", "_"); Categories.MarkerIcon = FileUpload.UploadFile(icon, filename, full_path); db.Entry(Categories).State = EntityState.Modified; db.SaveChanges(); } return RedirectToAction("Index"); } } catch (System.Data.Entity.Validation.DbEntityValidationException e) { var outputLines = new List<string>(); foreach (var eve in e.EntityValidationErrors) { outputLines.Add(string.Format( "Entity of type \"{1}\" in state \"{2}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State)); foreach (var ve in eve.ValidationErrors) { outputLines.Add(string.Format( "- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage)); } } foreach (var error in outputLines) HtmlHelpers.WriteErrorLogs(error); throw; } return View(Categories); }