// GET: Products/Create
 public ActionResult Create()
 {
     var product = new Product();
     product.ComplianceForms = new List<ComplianceForm>();
     PopulateAssignedComplianceFormData(product);
     return View();
 }
        private void UpdateProduct(string[] selectedComplainceForms, Product ProductToUpdate)
        {
            if (selectedComplainceForms == null)
            {
                ProductToUpdate.ComplianceForms = new List<ComplianceForm>();
                return;
            }

            var selectedComplianceFormsHS = new HashSet<string>(selectedComplainceForms);
            var ProductComplianceform = new HashSet<int>
                (ProductToUpdate.ComplianceForms.Select(c => c.ComplianceFormID));
            foreach (var complianceform in db.ComplianceForms)
            {
                if (selectedComplianceFormsHS.Contains(complianceform.ComplianceFormID.ToString()))
                {
                    if (!ProductComplianceform.Contains(complianceform.ComplianceFormID))
                    {
                        ProductToUpdate.ComplianceForms.Add(complianceform);
                    }
                }
                else
                {
                    if (ProductComplianceform.Contains(complianceform.ComplianceFormID))
                    {
                        ProductToUpdate.ComplianceForms.Remove(complianceform);
                    }
                }
            }
        }
 private void PopulateAssignedComplianceFormData(Product product)
 {
     var allcomplianceforms = db.ComplianceForms;
     var ProductComplianceForms = new HashSet<int>(product.ComplianceForms.Select(c => c.ComplianceFormID));
     var viewModel = new List<AssignedComplianceFormData>();
     foreach (var complianceform in allcomplianceforms)
     {
         viewModel.Add(new AssignedComplianceFormData
         {
             ComplianceFormID = complianceform.ComplianceFormID,
             Title = complianceform.Name,
             Assigned = ProductComplianceForms.Contains(complianceform.ComplianceFormID)
         });
     }
     ViewBag.Complinances = viewModel;
 }