public ActionResult Edit([Bind(Include = "CareCompanyId,CareCompany,SelectedCollectiveAgreementId,CollectiveAgreement")] CareCompanyEditVM careCompanyEditVM, string submitButton) { //ModelState.Remove(nameof(CareCompany.CollectiveAgreementSpecName)); if (db.CareCompanies.Where(c => (c.OrganisationNumber == careCompanyEditVM.CareCompany.OrganisationNumber && c.Id != careCompanyEditVM.CareCompany.Id)).Any()) { ModelState.AddModelError("", "Det finns redan ett bolag med det organisationsnumret."); } careCompanyEditVM.SelectedCollectiveAgreementId = careCompanyEditVM.CareCompany.SelectedCollectiveAgreementId; List <SelectListItem> collectiveAgreements = new List <SelectListItem>(); if (ModelState.IsValid) { if (submitButton == "Spara") { var careCompany = db.CareCompanies.Where(c => c.Id == careCompanyEditVM.CareCompanyId).FirstOrDefault(); careCompany.CompanyName = careCompanyEditVM.CareCompany.CompanyName; careCompany.OrganisationNumber = careCompanyEditVM.CareCompany.OrganisationNumber; careCompany.StreetAddress = careCompanyEditVM.CareCompany.StreetAddress; careCompany.Postcode = careCompanyEditVM.CareCompany.Postcode; careCompany.City = careCompanyEditVM.CareCompany.City; careCompany.CompanyPhoneNumber = careCompanyEditVM.CareCompany.CompanyPhoneNumber; careCompany.AccountNumber = careCompanyEditVM.CareCompany.AccountNumber; careCompany.CollectiveAgreementName = careCompanyEditVM.CareCompany.CollectiveAgreementName; careCompany.CollectiveAgreementSpecName = careCompanyEditVM.CareCompany.CollectiveAgreementSpecName; careCompany.SelectedCollectiveAgreementId = careCompanyEditVM.CareCompany.SelectedCollectiveAgreementId; db.Entry(careCompany).State = EntityState.Modified; db.SaveChanges(); //collectiveAgreements = db.CollectiveAgreementHeaders.ToList().ConvertAll(c => new SelectListItem //{ // Value = $"{c.Id}", // Text = c.Name //}); //careCompanyEditVM.CollectiveAgreement = new SelectList(collectiveAgreements, "Value", "Text"); //return View(careCompanyEditVM); } if (User.IsInRole("Ombud")) { return(RedirectToAction("Index", "Claims")); } // User is in role "Admin" return(RedirectToAction("Index")); } collectiveAgreements = db.CollectiveAgreementHeaders.ToList().ConvertAll(c => new SelectListItem { Value = $"{c.Id}", Text = c.Name }); careCompanyEditVM.CollectiveAgreement = new SelectList(collectiveAgreements, "Value", "Text"); return(View(careCompanyEditVM)); }
public ActionResult Edit(int?id) { if (id == null) { //return new HttpStatusCodeResult(HttpStatusCode.BadRequest); return(RedirectToAction("Index", "Home")); } CareCompany careCompany = db.CareCompanies.Find(id); if (careCompany == null) { return(HttpNotFound()); } // It should not be possible to update passive Companies if (User.IsInRole("Admin") && careCompany.IsActive == false) { return(RedirectToAction("Index")); } CareCompanyEditVM careCompanyEditVM = new CareCompanyEditVM(); careCompanyEditVM.CareCompany = careCompany; careCompanyEditVM.CareCompanyId = (int)id; List <SelectListItem> collectiveAgreements = new List <SelectListItem>(); collectiveAgreements = db.CollectiveAgreementHeaders.ToList().ConvertAll(c => new SelectListItem { Value = $"{c.Id}", Text = c.Name }); careCompanyEditVM.CollectiveAgreement = new SelectList(collectiveAgreements, "Value", "Text"); careCompanyEditVM.SelectedCollectiveAgreementId = careCompany.SelectedCollectiveAgreementId; return(View(careCompanyEditVM)); }