// GET: SecurityTeams/Edit/5 public async Task<ActionResult> Edit(int? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var securityTeamsViewModel = new SecurityTeamsVievModel { SecurityTeam = db.SecurityTeams.Include(i => i.SecureObjects).First(i => i.Id == id) }; if (securityTeamsViewModel.SecurityTeam == null) return HttpNotFound(); var allSecureObjectsList = db.SecureObjects.ToList(); securityTeamsViewModel.AllSecurityTeams = allSecureObjectsList.Select(o => new SelectListItem { Text = o.Name, Value = o.Id.ToString() }); ViewBag.EmployerID = new SelectList(db.Users, "Id", "FullName", securityTeamsViewModel.SecurityTeam.Name); return View(securityTeamsViewModel); }
[ValidateAntiForgeryToken]//[Bind(Include = "Title,Id,EmployerID,SelectedJobTags")] public ActionResult Edit(SecurityTeamsVievModel SecurityTeamsVievModel) { if (SecurityTeamsVievModel == null) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); if (ModelState.IsValid) { var jobToUpdate = db.SecurityTeams .Include(i => i.SecureObjects ).First(i => i.Id == SecurityTeamsVievModel.SecurityTeam.Id); if (TryUpdateModel(jobToUpdate, "SecurityTeam", new string[] { "Title", "EmployerID" })) { var newJobTags = db.SecureObjects.Where( m => SecurityTeamsVievModel.SelectedSecureObjects.Contains(m.Id)).ToList(); var updatedSecureObjects = new HashSet<int>(SecurityTeamsVievModel.SelectedSecureObjects); foreach (SecureObject secObj in db.SecureObjects) { if (!updatedSecureObjects.Contains(secObj.Id)) { jobToUpdate.SecureObjects.Remove(secObj); } else { jobToUpdate.SecureObjects.Add((secObj)); } } db.Entry(jobToUpdate).State = EntityState.Modified; db.SaveChanges(); } return RedirectToAction("Index"); } //ViewBag.EmployerID = new SelectList(db.Employers, "Id", "FullName", jobpostView.JobPost.EmployerID); return View(SecurityTeamsVievModel); }