public async Task <ActionResult> Create(Product_Info model, Guid[] attributeId = null, string[] attributeName = null, decimal[] attributePrice = null, string[] tagId = null) { if (ModelState.IsValid) { model.Id = Guid.NewGuid(); model.State = ProductState.SoldOut; //商品状态初始为下架 SetModelWithChangeStates(model, default(Guid?)); db.Product_Infos.Add(model); #region 保存商品标签 if (tagId != null) { for (var i = 0; i < tagId.Length; i++) { var pt = new Product_ProductTag(); pt.Id = Guid.NewGuid(); SetModelWithChangeStates(pt, default(Guid?)); pt.ProductId = model.Id; pt.TagId = new Guid(tagId[i]); db.Product_ProductTags.Add(pt); } } #endregion #region 保存商品属性 if (attributeId != null) { for (var i = 0; i < attributeId.Length; i++) { var pa = new Product_ProductAttribute(); pa.Id = Guid.NewGuid(); SetModelWithChangeStates(pa, default(Guid?)); pa.ProductId = model.Id; pa.AttributeName = attributeName[i]; pa.AttributeId = attributeId[i]; pa.AttributePrice = attributePrice[i]; pa.AttributeSort = i; db.Product_ProductAttributes.Add(pa); } } #endregion #region 创建商品相册 var photoGallery = new Site_PhotoGallery(); photoGallery.Id = model.Id; photoGallery.Title = "【" + model.Name + "】-商品相册"; SetModelWithChangeStates(photoGallery, default(Guid?)); db.Site_PhotoGallerys.Add(photoGallery); #endregion await db.SaveChangesAsync(); return(RedirectToActionPermanent("Images", new { id = model.Id })); } ViewBag.ResourcesCategoryId = GetCategoryList(null); ViewBag.ResourcesTypeId = GetTypeList(null); ViewBag.ResourcesTagId = getTagList(); return(View(model)); }
public async Task <ActionResult> Edit(Product_Info model, Guid[] attributeId = null, string[] attributeName = null, decimal[] attributePrice = null, string[] tagId = null) { if (ModelState.IsValid) { SetModelWithChangeStates(model, model.Id); #region 修改商品相册信息 var photoGallery = db.Site_PhotoGallerys.Find(model.Id); photoGallery.Title = "【" + model.Name + "】-商品相册"; SetModelWithChangeStates(photoGallery, photoGallery.Id); #endregion await db.SaveChangesAsync(); #region 保存商品标签 //先删除所有商品表情 var tagModels = await db.Product_ProductTags.Where(p => p.ProductId == model.Id).ToListAsync(); if (tagModels.Count != 0) { db.Product_ProductTags.RemoveRange(tagModels); await db.SaveChangesAsync(); } if (tagId != null) { for (var i = 0; i < tagId.Length; i++) { var pt = new Product_ProductTag(); pt.Id = Guid.NewGuid(); SetModelWithChangeStates(pt, default(Guid?)); pt.ProductId = model.Id; pt.TagId = new Guid(tagId[i]); db.Product_ProductTags.Add(pt); } } await db.SaveChangesAsync(); #endregion #region 保存商品属性 //先删除所有商品属性 var attrModels = await db.Product_ProductAttributes.Where(p => p.ProductId == model.Id).ToListAsync(); if (attrModels.Count != 0) { db.Product_ProductAttributes.RemoveRange(attrModels); await db.SaveChangesAsync(); } if (attributeId != null) { for (var i = 0; i < attributeId.Length; i++) { var pa = new Product_ProductAttribute(); pa.Id = Guid.NewGuid(); SetModelWithChangeStates(pa, default(Guid?)); pa.ProductId = model.Id; pa.AttributeId = attributeId[i]; pa.AttributeName = attributeName[i]; pa.AttributePrice = attributePrice[i]; pa.AttributeSort = i; db.Product_ProductAttributes.Add(pa); } } await db.SaveChangesAsync(); #endregion return(RedirectToAction("Index")); } return(View(model)); }