public ActionResult Edit(long id) { GiftViewModel model = new GiftViewModel(); GiftInfo data = new GiftInfo(); data = _iGiftService.GetById(id); if (data == null) { throw new MallException("错误的礼品编号。"); } model = data.Map <GiftViewModel>(); //补充图片数据 if (string.IsNullOrWhiteSpace(model.ImagePath)) { model.ImagePath = string.Format(@"/Storage/Gift/{0}", id); } string path = model.ImagePath; string paths = model.ImagePath; string _imgpath = paths + "/1.png"; if (MallIO.ExistFile(_imgpath)) { model.PicUrl1 = Core.MallIO.GetImagePath(path + "/1.png"); } _imgpath = paths + "/2.png"; if (MallIO.ExistFile(_imgpath)) { model.PicUrl2 = Core.MallIO.GetImagePath(path + "/2.png"); } _imgpath = paths + "/3.png"; if (MallIO.ExistFile(_imgpath)) { model.PicUrl3 = Core.MallIO.GetImagePath(path + "/3.png"); } _imgpath = paths + "/4.png"; if (MallIO.ExistFile(_imgpath)) { model.PicUrl4 = Core.MallIO.GetImagePath(path + "/4.png"); } _imgpath = paths + "/5.png"; if (MallIO.ExistFile(_imgpath)) { model.PicUrl5 = Core.MallIO.GetImagePath(path + "/5.png"); } #region 会员等级列表 List <SelectListItem> MemGradeSelList = GetMemberGradeSelectList(model.NeedGrade); ViewBag.MemberGradeSelect = MemGradeSelList; #endregion return(View(model)); }
public JsonResult Edit(GiftViewModel model) { var result = new AjaxReturnData { success = false, msg = "未知错误" }; if (ModelState.IsValid) { GiftViewModel postdata = new GiftViewModel(); if (model.Id > 0) { GiftInfo dbdata = _iGiftService.GetByIdAsNoTracking(model.Id); //数据补充 if (dbdata == null) { result.success = false; result.msg = "编号有误"; return(Json(result)); } postdata = dbdata.Map <GiftViewModel>(); } else { if (model.StockQuantity < 1) { result.success = false; result.msg = "库存必须大于0"; return(Json(result)); } } // UpdateModel(postdata); GiftInfo data = new GiftInfo(); data = postdata.Map <GiftInfo>(); if (model.Id > 0) { _iGiftService.UpdateGift(data); } else { data.Sequence = 100; data.AddDate = DateTime.Now; data.SalesStatus = GiftInfo.GiftSalesStatus.Normal; _iGiftService.AddGift(data); } #region 转移图片 int index = 1; List <string> piclist = new List <string>(); piclist.Add(model.PicUrl1); piclist.Add(model.PicUrl2); piclist.Add(model.PicUrl3); piclist.Add(model.PicUrl4); piclist.Add(model.PicUrl5); string path = data.ImagePath; foreach (var item in piclist) { if (!string.IsNullOrWhiteSpace(item)) { string source = string.Empty; if (item.IndexOf("temp/") > 0) { source = item.Substring(item.LastIndexOf("/temp")); } else if (item.Contains(data.ImagePath)) { source = item.Substring(item.LastIndexOf(data.ImagePath)); } try { string dest = string.Format("{0}/{1}.png", path, index); if (source == dest) { index++; continue; } if (!string.IsNullOrWhiteSpace(source)) { Core.MallIO.CopyFile(source, dest, true); } var imageSizes = EnumHelper.ToDictionary <ImageSize>().Select(t => t.Key); foreach (var imageSize in imageSizes) { string size = string.Format("{0}/{1}_{2}.png", path, index, imageSize); Core.MallIO.CreateThumbnail(dest, size, imageSize, imageSize); } //using (Image image = Image.FromFile(source)) //{ // image.Save(dest, System.Drawing.Imaging.ImageFormat.Png); // var imageSizes = EnumHelper.ToDictionary<GiftInfo.ImageSize>().Select(t => t.Key); // foreach (var imageSize in imageSizes) // { // string size = string.Format("{0}/{1}_{2}.png", path, index, imageSize); // ImageHelper.CreateThumbnail(dest, size, imageSize, imageSize); // } //} index++; } catch (FileNotFoundException fex) { index++; Core.Log.Error("发布礼品时候,没有找到文件", fex); } catch (System.Runtime.InteropServices.ExternalException eex) { index++; Core.Log.Error("发布礼品时候,ExternalException异常", eex); } catch (Exception ex) { index++; Core.Log.Error("发布礼品时候,Exception异常", ex); } } else { string dest = string.Format("{0}/{1}.png", path, index); if (MallIO.ExistFile(dest)) { MallIO.DeleteFile(dest); } var imageSizes = EnumHelper.ToDictionary <ImageSize>().Select(t => t.Key); foreach (var imageSize in imageSizes) { string size = string.Format("{0}/{1}_{2}.png", path, index, imageSize); if (MallIO.ExistFile(size)) { MallIO.DeleteFile(size); } } index++; } } #endregion result.success = true; result.msg = "操作成功"; } else { result.success = false; result.msg = "数据有误"; } return(Json(result)); }