public JsonResult AddProductRedeem(AddProductRedeemViewModel model) { using (var db = new TourEntities()) { if (ModelState.IsValid) { var formsAuthentication = HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName] != null ? FormsAuthentication.Decrypt( HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName].Value) : null; string imageName = System.IO.Path.GetFileName(model.ImagePath.FileName); imageName = MetadataServices.GetDateTimeWithoutSlash() + "-" + imageName; string physicalPath = Server.MapPath("~/Image/Product/" + imageName); model.ImagePath.SaveAs(physicalPath); var newProductRedeemViewModel = new ProductRedeem() { ProductRedeemName = model.ProductRedeemName, RedeemPoint = model.RedeemPoint, Stock = model.Stock, ImagePath = imageName, CreatedBy = MetadataServices.GetCurrentUser().Username, CreatedAt = MetadataServices.GetCurrentDate(), UpdatedBy = MetadataServices.GetCurrentUser().Username, UpdatedAt = MetadataServices.GetCurrentDate() }; db.ProductRedeem.Add(newProductRedeemViewModel); db.SaveChanges(); } return(Json(new { }, JsonRequestBehavior.AllowGet)); } }
public JsonResult EditProductRedeem(AddProductRedeemViewModel model) { try { using (var db = new TourEntities()) { var formsAuthentication = HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName] != null ? FormsAuthentication.Decrypt( HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName].Value) : null; if (ModelState.IsValid) { var productRedeem = db.ProductRedeem.Find(model.ProductRedeemId); if (model.ImagePath != null) { FileInfo path = new FileInfo(Server.MapPath("~/Image/Product/" + model.ImagePath)); path.Delete(); string imageName = System.IO.Path.GetFileName(model.ImagePath.FileName); imageName = MetadataServices.GetDateTimeWithoutSlash() + "-" + imageName; string physicalPath = Server.MapPath("~/Image/Product/" + imageName); model.ImagePath.SaveAs(physicalPath); productRedeem.ImagePath = imageName; } productRedeem.ProductRedeemName = model.ProductRedeemName; productRedeem.Stock = model.Stock; productRedeem.RedeemPoint = model.RedeemPoint; productRedeem.UpdatedBy = MetadataServices.GetCurrentUser().Username; productRedeem.UpdatedAt = MetadataServices.GetCurrentDate(); db.SaveChanges(); } return(Json(new { }, JsonRequestBehavior.AllowGet)); } } catch (Exception ex) { throw ex; } }