public ActionResult Products() { ProductHouseModel model = new ProductHouseModel(); List <ProductToProductLineModel> phModel = new List <ProductToProductLineModel>(); using (var mm = new MaterialsManager()) { List <Product> products = mm.ActiveProducts().ToList(); List <ProductLine> partsOfHouse = mm.ActiveProductLines().ToList(); List <ProductToLine> prodHouses = mm.AllProductToProductLine().ToList(); model.Products = products.AsEnumerable(); model.PartsOfHouse = partsOfHouse.AsEnumerable(); // create the list based on our product and part list foreach (var partOfHouse in partsOfHouse) { foreach (var product in products) { phModel.Add(new ProductToProductLineModel { IsActive = prodHouses.Any(ph => ph.ProductID == product.ID && ph.ProductLineID == partOfHouse.ID) && prodHouses.First(ph => ph.ProductID == product.ID && ph.ProductLineID == partOfHouse.ID).IsActive, ProductID = product.ID, ProductLineID = partOfHouse.ID, ProductName = product.Name, ProductLineName = partOfHouse.Name }); } } model.ProductPartHouse = phModel.AsEnumerable(); } return(View("Products", model)); }
public ActionResult Products(ProductHouseModel model) { List <ProductToLine> parts = new List <ProductToLine>(); // loop through to create a list and then save foreach (var part in model.ProductPartHouse.Where(m => m.IsActive)) { parts.Add(new ProductToLine { IsActive = true, ProductLineID = part.ProductLineID, ProductID = part.ProductID }); } // first delete all of the existing ones using (var mm = new MaterialsManager()) { mm.DeleteProductToProductLine(); mm.CreateProductToProductLine(parts); } return(RedirectToAction("Index", "Home")); }