public ActionResult Update(GoodsModel model) { if (model == null) { return(RedirectToAction("List", new { msg = "更新失败" })); } // 更新 Goods 本身 #region 更新goods本身 Goods goods = _goodsBLL.GetGoodsByCode(model.Code); goods.Price = model.Price; goods.Description = model.Description; goods.ServiceCount = model.ServiceCount; // 商品添加新的字段 OriginalPrice 更新时 也要更新 goods.OriginalPrice = model.OriginalPrice; if (!_goodsBLL.Update(goods)) { return(RedirectToAction("List", new { msg = "更新商品失败" })); } #endregion // 更新 商品图片 #region 更新商品图片 GoodsImage gi = _goodsImagesBLL.GetPictureByGoodsId(goods.Id); if (gi.ImagePath != model.PicturePath) { gi.ImagePath = model.PicturePath; } if (!_goodsImagesBLL.Update(gi)) { return(RedirectToAction("List", new { msg = "更新商品图片失败" })); } #endregion // 更新商品分类的时候 先删除原先的分类 // 再根据新的 model.CategoryList 重新添加上分类 #region 更新 商品分类列表 if (_goodsCategoryBLL.RemoveGoodsCategoryByGoodsId(goods.Id)) { foreach (var item in model.CategoryList) { // 构造 GoodsCategory 对象 GoodsCategory gc = new GoodsCategory(); gc.Id = Guid.NewGuid(); gc.CreatedTime = DateTime.Now.Date; gc.CategoryId = _categoryBLL.GetCategoryByCategoryNo(item).Id; gc.IsDeleted = false; gc.GoodsId = goods.Id; _goodsCategoryBLL.Add(gc); } } #endregion return(RedirectToAction("List", new { msg = "更新成功" })); }