public ActionResult SaveCompanyAndProductRelation(CompanyProfAndCompanyProductRelationFormViewModel viewModel) { //if (!ModelState.IsValid) // return new HttpStatusCodeResult(HttpStatusCode.BadRequest); ChemAnalystContext _context = new ChemAnalystContext(); foreach (var item in viewModel.CompanyProduct) { if (item.IsSelected) { var checkForSameCompany = _context.CompanyAndProductRelations.Where(w => w.SA_CompanyId == viewModel.CompanyProfId && w.SA_ProductId == item.id).FirstOrDefault(); if (checkForSameCompany != null) { TempData["Message"] = "This compnay already exist with same product."; } else { CompanyAndProductRelation companyAndProductRelation = new CompanyAndProductRelation(); companyAndProductRelation.SA_CompanyId = viewModel.CompanyProfId; companyAndProductRelation.SA_ProductId = item.id; _context.CompanyAndProductRelations.Add(companyAndProductRelation); } } } _context.SaveChanges(); //TEMP REDIRECT. return(RedirectToAction("AllComAndProductRelationRecords")); }
// GET: CompanyAndProductRelation public ActionResult Index() { ChemAnalystContext _context = new ChemAnalystContext(); var viewModel = new CompanyProfAndCompanyProductRelationFormViewModel() { CompanyProf = _context.SA_Company.ToList(), CompanyProduct = _context.SA_Product.ToList() }; return(View(viewModel)); }