// GET: EnrollDepartments/Create public ActionResult Create() { ViewBag.Campid = new SelectList(_work.Campus.GetAll(), "Campid", "Camp_name"); var Alldep = _work.Department.GetAll(); AddCampusVM vm = new AddCampusVM(); var checkbox = new List<CheckBoxListItem>(); foreach (var dep in Alldep) { checkbox.Add(new CheckBoxListItem() { ID = dep.Depid, Display = dep.Dep_name, IsChecked = false }); } vm.Dep = checkbox; return View(vm); }
public ActionResult Create( AddCampusVM campus) { int count = 0; IEnumerable<EnrollDepartment> EnrollDep = _work.EnrollDep.Find(x => x.Campid == campus.Campid); List<EnrollDepartment> DeleteList = new List<EnrollDepartment>(); List<EnrollDepartment> DepList = new List<EnrollDepartment>(); List<EnrollDepartment> InsertList = new List<EnrollDepartment>(); if (ModelState.IsValid) { try { foreach (var Dep1 in campus.Dep) { count = 0; foreach (var Dep2 in EnrollDep) { if (Dep1.ID == Dep2.Depid) { count = 1; if (Dep1.IsChecked == false) { DeleteList.Add(Dep2); } break; } } if (count == 0) // if department already not added { if (Dep1.IsChecked == true) { var obj = new EnrollDepartment(); obj.Campid = campus.Campid; obj.Depid = Dep1.ID; obj.EnrollDate = campus.EnrollDate; InsertList.Add(obj); } } } _work.EnrollDep.InsertRange(InsertList); foreach (var value in DeleteList) { IEnumerable<Teacher> teach= _work.Teacher.Find(x=>x.DepEnrolId==x.DepEnrolId); foreach(var t in teach) { var file = _work.File.GetById(t.PersonId); _work.File.Delete(file); } _work.Teacher.DeleteRange(teach); _work.EnrollDep.DeleteRange(DeleteList); } _work.Save(); } catch (Exception exe) { ViewBag.Error = "Unable to Update Campus"; } } return RedirectToAction("Index"); }
public ActionResult GetDep(int camp) { int count = 0; Campus campus = _work.Campus.GetById(camp); IEnumerable<EnrollDepartment> EnrollDep = _work.EnrollDep.Find(x => x.Campid == camp); var Alldep = _work.Department.GetAll(); AddCampusVM vm = new AddCampusVM(); var checkbox = new List<CheckBoxListItem>(); foreach (var dep in Alldep) { foreach (var dep2 in EnrollDep) { if (dep.Depid == dep2.Depid) { checkbox.Add(new CheckBoxListItem() { ID = dep.Depid, Display = dep.Dep_name, IsChecked = true }); count = 1; break; } } if (count == 0) { checkbox.Add(new CheckBoxListItem() { ID = dep.Depid, Display = dep.Dep_name, IsChecked = false }); } count = 0; } vm.Dep = checkbox; return PartialView("GetDep", vm); }