public ActionResult CategoryPartial() { try { var lstCategory = db.Categories.ToList(); ViewBag.lstCategory = lstCategory; List<Product> lstProduct = new List<Product>(); Product product = new Product(); foreach (var item in lstCategory) { product = db.Products.FirstOrDefault(n => n.CategoryId == item.CategoryId); if (product != null) { lstProduct.Add(product); } } ViewBag.lstProduct = lstProduct; return PartialView(); } catch { return RedirectToAction("Index", "Error"); } }
public bool AddProduct(string productName, string productUnit, double productWeight, string productDes, string productNote, int productPrice, int dropCate, string productCode, string productImage, int[] materialId, int[] materialQuantity) { DbContextTransaction contextTransaction = db.Database.BeginTransaction(); Product product = new Product(); product.ProductName = productName; product.Unit = productUnit; product.ProductWeight = productWeight; if (productDes != null) { product.Descriptions = productDes; } if (productNote != null) { product.Note = productNote; } product.ProductStandardPrice = productPrice; product.CategoryId = dropCate; product.ProductCode = productCode; if (productImage != null) { product.ProductImage = productImage; } product.IsActive = true; db.Products.Add(product); db.SaveChanges(); int productId = GetProductId(); for (int i = 0; i < materialId.Length; i++) { Recipe recipe = new Recipe(); recipe.ProductId = productId; recipe.ProductMaterialId = materialId[i]; recipe.RecipeQuantity = materialQuantity[i]; db.Recipes.Add(recipe); db.SaveChanges(); } try { contextTransaction.Commit(); } catch (Exception) { contextTransaction.Rollback(); } finally { contextTransaction.Dispose(); } return true; }
public int AddTempProductInfor(string productName, string productUnit, double? productWeight, string productDes, string productNote, int? productPrice, int dropCate, string fileName, int[] materialId, int[] materialQuantity) { try { Product product = new Product(); product.ProductName = productName; product.Unit = productUnit; if (productWeight == null) { product.ProductWeight = -1; } else { product.ProductWeight = productWeight.Value; } product.Descriptions = productDes; product.Note = productNote; if (productPrice == null) { product.ProductStandardPrice = -1; } else { product.ProductStandardPrice = productPrice.Value; } product.CategoryId = dropCate; product.ProductImage = fileName; Session["ProductInfor"] = product; ManageProductBusiness mpb = new ManageProductBusiness(); List<AllProductInfoViewModel> apivList = new List<AllProductInfoViewModel>(); if (materialId != null && materialQuantity != null && materialQuantity.Length > 0 && materialId.Length > 0) { for (int i = 0; i < materialId.Length; i++) { var pm = mpb.GetMaterialById(materialId[i]); AllProductInfoViewModel apiv = new AllProductInfoViewModel { MaterialId = pm.ProductMaterialId, MaterialName = pm.ProductMaterialName, MaterialUnit = pm.ProductMaterialUnit, MaterialQuantity = materialQuantity[i] }; apivList.Add(apiv); } } if (apivList.Count > 0) { Session["Material"] = apivList; } return 1; } catch { return -1; } }