public ActionResult MaterialProductsGrid(MaterialProductGridModel model) { List<MaterialToProduct> mpAdd = new List<MaterialToProduct>(); List<int> mpDelete = new List<int>(); foreach (var mp in model.MaterialProducts) { if (mp.IsActive) { // we only need to add new ones since nothing will change with existing ones if (mp.MaterialToProductID == 0) { mpAdd.Add(new MaterialToProduct { ID = mp.MaterialToProductID, MaterialID = mp.MaterialID, ProductID = mp.ProductID }); } } else { // only need to delete ones that already exist in the database if (mp.MaterialToProductID > 0) { mpDelete.Add(mp.MaterialToProductID); } } } // now update the database by deleting ones and updating others using (var mm = new MaterialsManager()) { mm.DeleteMaterialToProducts(mpDelete); mm.InsertMaterialToProducts(mpAdd); } return RedirectToAction("Index"); }
public ActionResult MaterialProductsGrid(int? supplierID) { using (var um = new UserManager()) { var currentUser = um.ByUsername(User.Identity.Name); if (currentUser.SupplierID != null) { supplierID = currentUser.SupplierID.GetValueOrDefault(); } } var model = new MaterialProductGridModel(); var materialToProductModels = new List<MaterialToProductModel>(); using (var mm = new MaterialsManager()) { var products = mm.ActiveProducts().ToList(); var materials = mm.BySupplier(supplierID.GetValueOrDefault()).ToList(); var materialToProducts = mm.ByManufacturerID(supplierID.GetValueOrDefault()).ToList(); model.Products = products.AsEnumerable(); model.Materials = materials.AsEnumerable(); // create the list based on our product and material list foreach (var material in materials) { foreach (var product in products) { materialToProductModels.Add(new MaterialToProductModel { IsActive = materialToProducts.Any(mp => mp.ProductID == product.ID && mp.MaterialID == material.ID), ProductID = product.ID, MaterialID = material.ID, ProductName = product.Name, MaterialName = material.Name, MaterialToProductID = materialToProducts.Any(mp => mp.ProductID == product.ID && mp.MaterialID == material.ID) ? materialToProducts.First(mp => mp.ProductID == product.ID && mp.MaterialID == material.ID).ID : 0 }); } } model.MaterialProducts = materialToProductModels.AsEnumerable(); } return View("MaterialProductsGrid", model); }