public ActionResult Edit(int classId = 0, int teacherId = 0)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.StudyClassTeacher.Edit();
                vm.WeekList    = Basis.Controllers.WeekController.SelectList();
                vm.TeacherList = Teacher.Controllers.TeacherController.SelectList();
                vm.ClassList   = StudyClassController.SelectList(vm.StudyId);
                if (classId != 0 && teacherId != 0 && vm.StudyId != 0)
                {
                    vm.StudyClassTeacherEdit = (from p in db.Table <Study.Entity.tbStudyClassTeacher>()
                                                where p.tbStudy.Id == vm.StudyId &&
                                                p.tbClass.Id == classId &&
                                                p.tbTeacher.Id == teacherId &&
                                                p.tbClass.IsDeleted == false &&
                                                p.tbStudy.IsDeleted == false &&
                                                p.tbTeacher.IsDeleted == false
                                                select new Dto.StudyClassTeacher.Edit
                    {
                        Id = p.Id,
                        IsMaster = p.IsMaster
                    }).FirstOrDefault();

                    vm.WeekIdList = (from p in db.Table <Study.Entity.tbStudyClassTeacher>()
                                     where p.tbStudy.Id == vm.StudyId &&
                                     p.tbClass.Id == classId &&
                                     p.tbTeacher.Id == teacherId &&
                                     p.tbClass.IsDeleted == false &&
                                     p.tbStudy.IsDeleted == false &&
                                     p.tbTeacher.IsDeleted == false
                                     select p.tbWeek.Id).Distinct().ToList();
                }
                return(View(vm));
            }
        }
        public ActionResult Edit(Models.StudyClassTeacher.Edit vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var error = new List <string>();
                if (error.Count == decimal.Zero)
                {
                    var weekIds = new List <int>();
                    if (Request["CboxWeek"] != null)
                    {
                        weekIds = Request["CboxWeek"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(d => d.ConvertToInt()).ToList();
                    }

                    var tbStudyClassTeacherList = (from p in db.Table <Study.Entity.tbStudyClassTeacher>()
                                                   .Include(d => d.tbClass)
                                                   .Include(d => d.tbTeacher)
                                                   .Include(d => d.tbWeek)
                                                   where p.tbClass.Id == vm.ClassId &&
                                                   p.tbStudy.Id == vm.StudyId &&
                                                   p.tbTeacher.Id == vm.TeacherId &&
                                                   p.tbClass.IsDeleted == false &&
                                                   p.tbStudy.IsDeleted == false &&
                                                   p.tbTeacher.IsDeleted == false
                                                   select p).ToList();

                    foreach (var a in tbStudyClassTeacherList.Where(d => weekIds.Contains(d.tbWeek.Id) == false))
                    {
                        a.IsDeleted = true;
                    }

                    foreach (var a in weekIds.Where(d => tbStudyClassTeacherList.Select(q => q.tbWeek.Id).Contains(d) == false))
                    {
                        var tb = new Study.Entity.tbStudyClassTeacher();
                        tb.tbTeacher = db.Set <Teacher.Entity.tbTeacher>().Find(vm.TeacherId);
                        tb.tbClass   = db.Set <Basis.Entity.tbClass>().Find(vm.ClassId);
                        tb.tbWeek    = db.Set <Basis.Entity.tbWeek>().Find(a);
                        tb.tbStudy   = db.Set <Study.Entity.tbStudy>().Find(vm.StudyId);
                        tb.IsMaster  = vm.StudyClassTeacherEdit.IsMaster;
                        db.Set <Study.Entity.tbStudyClassTeacher>().Add(tb);
                    }

                    foreach (var a in tbStudyClassTeacherList.Where(d => weekIds.Contains(d.tbWeek.Id) == true))
                    {
                        a.IsMaster = vm.StudyClassTeacherEdit.IsMaster;
                    }

                    if (db.SaveChanges() > 0)
                    {
                        if (vm.StudyClassTeacherEdit.IsMaster)
                        {
                            var tbStudyClassTeacher = (from p in db.Table <Study.Entity.tbStudyClassTeacher>()
                                                       .Include(d => d.tbClass)
                                                       where p.tbClass.Id == vm.ClassId &&
                                                       p.tbStudy.Id == vm.StudyId &&
                                                       p.tbTeacher.Id != vm.TeacherId &&
                                                       p.tbClass.IsDeleted == false &&
                                                       p.tbStudy.IsDeleted == false &&
                                                       p.tbTeacher.IsDeleted == false
                                                       select p).ToList();
                            foreach (var a in tbStudyClassTeacher)
                            {
                                a.IsMaster = false;
                            }
                            db.SaveChanges();
                        }
                        XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改晚自习教管");
                    }
                }
                return(Code.MvcHelper.Post(null, Url.Action("List", new { searchText = vm.SearchText, studyId = vm.StudyId })));
            }
        }