public ActionResult UpdateProduct(int id, string productName) { string errMsg = "更新成功"; if (string.IsNullOrEmpty(productName)) { errMsg = "产品名称不能为空"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } IBase_ProductInfoService base_ProductInfoService = BLLContainer.Resolve <IBase_ProductInfoService>(); if (base_ProductInfoService.GetCount(p => p.ID != id && p.ProductName.Equals(productName, StringComparison.OrdinalIgnoreCase)) > 0) { errMsg = "该产品名称已存在"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } var base_ProductInfo = base_ProductInfoService.GetModels(p => p.ID == id).FirstOrDefault(); base_ProductInfo.ProductName = productName; if (!base_ProductInfoService.Update(base_ProductInfo)) { errMsg = "数据库更新失败"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } //更新缓存 ProductTypeBusiness.Init(); return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); }
public ActionResult CreateProduct(string productName) { string errMsg = "添加成功"; if (string.IsNullOrEmpty(productName)) { errMsg = "产品名称不能为空"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } //开启EF事务 using (TransactionScope ts = new TransactionScope()) { try { IBase_ProductInfoService base_ProductInfoService = BLLContainer.Resolve <IBase_ProductInfoService>(); if (base_ProductInfoService.GetCount(p => p.ProductName.Equals(productName, StringComparison.OrdinalIgnoreCase)) > 0) { errMsg = "该产品名称已存在"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } var base_ProductInfo = new Base_ProductInfo(); base_ProductInfo.ProductName = productName; if (!base_ProductInfoService.Add(base_ProductInfo)) { errMsg = "数据库更新失败"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } var dbContext = DbContextFactory.CreateByModelNamespace(typeof(Base_ProductInfo).Namespace); var userLvls = DictSystemBusiness.GetDicts("客户等级"); foreach (var model in userLvls) { var base_ProductInfo_Detail = new Base_ProductInfo_Detail(); base_ProductInfo_Detail.UserLvl = Convert.ToInt32(model.DictValue); base_ProductInfo_Detail.ProductType = base_ProductInfo.ID; dbContext.Entry(base_ProductInfo_Detail).State = EntityState.Added; } if (dbContext.SaveChanges() < 0) { errMsg = "数据库更新失败"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } ts.Complete(); } catch (Exception ex) { return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = ex.Message })); } } //更新缓存 ProductTypeBusiness.Init(); return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); }
public ActionResult DeleteProduct(int id) { string errMsg = "删除成功"; //开启EF事务 using (TransactionScope ts = new TransactionScope()) { try { IBase_ProductInfoService base_ProductInfoService = BLLContainer.Resolve <IBase_ProductInfoService>(); if (base_ProductInfoService.GetCount(p => p.ID == id) == 0) { errMsg = "该产品信息不存在"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } var base_ProductInfo = base_ProductInfoService.GetModels(p => p.ID == id).FirstOrDefault(); if (!base_ProductInfoService.Delete(base_ProductInfo)) { errMsg = "数据库更新失败"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } IBase_ProductInfo_DetailService base_ProductInfo_DetailService = BLLContainer.Resolve <IBase_ProductInfo_DetailService>(); var dbContext = DbContextFactory.CreateByModelNamespace(typeof(Base_ProductInfo_Detail).Namespace); var base_ProductInfo_Details = base_ProductInfo_DetailService.GetModels(p => p.ProductType == base_ProductInfo.ID).ToList(); foreach (var item in base_ProductInfo_Details) { dbContext.Entry(item).State = System.Data.Entity.EntityState.Deleted; } if (dbContext.SaveChanges() < 0) { errMsg = "数据库更新失败"; return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); } ts.Complete(); } catch (Exception ex) { return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = ex.Message })); } } //更新缓存 ProductTypeBusiness.Init(); return(RedirectToAction("Index", "ProductInfoManage", new { errMsg = errMsg })); }