public ActionResult Create() { var model = new ProductModel(); model.ListCategories = GetListCategory(); return View(model); }
public ActionResult Create(ProductModel model) { try { if (ModelState.IsValid) { model.CreatedDate = DateTime.Now; model.UpdatedDate = DateTime.Now; model.CreatedBy = User.Identity.Name; model.UpdatedBy = User.Identity.Name; model.IsActive = true; model.Alias = Globals.GenerateAlias(model.Name, true); #region upload file image to server try { if (_logoModel != null && !string.IsNullOrEmpty(_logoModel.FileName)) { model.Picture = _logoModel.FileName; var srcFile = Path.Combine(Server.MapPath(Globals.TempImagePath(_logoModel.FileName))); var desFile = Path.Combine(Server.MapPath(Globals.ImagePath(ConstantKeys.ProductFolder, _logoModel.FileName))); // delete image if exist IOUtility.DeleteFile(desFile); // get temp file string tempFile = Server.MapPath(Globals.TempImagePath(_logoModel.FileName)); // get folder thumbs string thumbFolder = Server.MapPath(Globals.ThumbPath(ConstantKeys.ProductFolder, _logoModel.FileName)); // get folder image string imageFolder = Server.MapPath(Globals.ImagePath(ConstantKeys.ProductFolder, _logoModel.FileName)); // create thumb image System.Drawing.Image img = ImageUtility.ScaleImage(tempFile, 208, 180, "#ffffff"); ImageUtility.SaveImage(thumbFolder, img); img.Dispose(); // create image img = ImageUtility.ScaleImage(tempFile, 200, 200, "#ffffff"); ImageUtility.SaveImage(imageFolder, img); img.Dispose(); // delete temp file IOUtility.DeleteFile(srcFile); //// move full images //IOUtility.MoveFile(srcFile, desFile); } else model.Picture = "default.jpg"; _logoModel = null; } catch (Exception ex) { model.Picture = "default.jpg"; ex.ToString(); } #endregion var entity = model.ToEntity(); entity.ObjectState = ObjectState.Added; _productService.Insert(entity); _unitOfWork.SaveChanges(); return RedirectToAction("Index"); } else { return View(model); } } catch { return View(); } }
public string Edit(ProductModel model) { if (ModelState.IsValid) { var entity = _productService.Find(model.Id); if (entity != null) { entity.UpdatedDate = DateTime.Now; if (entity.Name != model.Name) { entity.Name = model.Name; entity.Alias = Globals.GenerateAlias(model.Name, true); } if (entity.Brief != model.Description) entity.Brief = model.Description; if (entity.Description != model.Content) entity.Description = model.Content; if (entity.CategoryId != model.CategoryId) entity.CategoryId = model.CategoryId; if (entity.Price != model.Price) entity.Price = model.Price; if (entity.Unit != model.Unit) entity.Unit = model.Unit; if (entity.IsActive != model.IsActive) entity.IsActive = model.IsActive; //if (entity.IsFeature != model.ShowIndex) // entity.IsFeature = model.ShowIndex; if (entity.Origin != model.Origin) entity.Origin = model.Origin; if (entity.CallPrice != model.CallPrice) entity.CallPrice = model.CallPrice; #region upload image if (_logoModel != null && !string.IsNullOrEmpty(_logoModel.FileName)) { //delete file old if (entity.Thumbnail != "default.jpg") { var pathOld = Globals.ImagePath(entity.Thumbnail); IOUtility.DeleteFile(pathOld); } entity.Thumbnail = _logoModel.FileName; var srcFile = Path.Combine(Server.MapPath(Globals.TempImagePath(_logoModel.FileName))); var desFile = Path.Combine(Server.MapPath(Globals.ImagePath(ConstantKeys.ProductFolder, _logoModel.FileName))); // delete image if exist IOUtility.DeleteFile(desFile); // get temp file string tempFile = Server.MapPath(Globals.TempImagePath(_logoModel.FileName)); // get folder thumbs string thumbFolder = Server.MapPath(Globals.ThumbPath(ConstantKeys.ProductFolder, _logoModel.FileName)); // get folder image string imageFolder = Server.MapPath(Globals.ImagePath(ConstantKeys.ProductFolder, _logoModel.FileName)); // create thumb image System.Drawing.Image img = ImageUtility.ScaleImage(tempFile, 208, 180, "#ffffff"); ImageUtility.SaveImage(thumbFolder, img); img.Dispose(); // create image img = ImageUtility.ScaleImage(tempFile, 200, 200, "#ffffff"); ImageUtility.SaveImage(imageFolder, img); img.Dispose(); // delete temp file IOUtility.DeleteFile(srcFile); //// move full images //IOUtility.MoveFile(srcFile, desFile); } _logoModel = null; #endregion entity.ObjectState = ObjectState.Modified; _productService.Update(entity); _unitOfWork.SaveChanges(); } return JsonConvert.SerializeObject(new { Status = 1, Message = "Update success." }); } else { return JsonConvert.SerializeObject(new { Status = 0, Message = "Update unsuccess." }); } }