public ActionResult List(Models.ElectiveInputByBase.List vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var error = new List <string>();

                var orgIds = new List <int>();
                if (Request["CboxOrg"] != null)
                {
                    orgIds = Request["CboxOrg"].Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(d => d.ConvertToInt()).ToList();
                }

                var result = Save(db, vm.ElectiveId, orgIds);
                if (string.IsNullOrEmpty(result) == false)
                {
                    error.Add(result);
                }

                return(Code.MvcHelper.Post(error, Url.Action("List", new { electiveId = vm.ElectiveId }), "提交成功!"));
            }
        }
        public ActionResult Modal()
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.ElectiveInputByBase.List();
                vm.Student = (from p in db.Table <Student.Entity.tbStudent>()
                              where p.tbSysUser.Id == Code.Common.UserId
                              select new Student.Dto.Student.SelectStudent()
                {
                    StudentCode = p.StudentCode,
                    StudentName = p.StudentName
                }).FirstOrDefault();

                var elective = (from p in db.Table <Entity.tbElective>()
                                where p.Id == vm.ElectiveId
                                select new
                {
                    p.ElectiveName,
                    p.FromDate,
                    p.ToDate,
                    p.tbElectiveType.ElectiveTypeCode
                }).FirstOrDefault();
                if (elective != null)
                {
                    vm.IsWeekPeriod = elective.ElectiveTypeCode == Code.EnumHelper.ElectiveType.WeekPeriod;
                    vm.ElectiveName = elective.ElectiveName;
                    vm.IsOpen       = elective.FromDate < DateTime.Now;
                    vm.IsEnd        = elective.ToDate < DateTime.Now;
                }

                var limitOrgList = (from p in db.Table <Entity.tbElectiveOrgClass>()
                                    join q in db.Table <Basis.Entity.tbClassStudent>() on p.tbClass.Id equals q.tbClass.Id
                                    where p.tbElectiveOrg.tbElective.Id == vm.ElectiveId &&
                                    p.tbElectiveOrg.IsDeleted == false &&
                                    q.tbStudent.IsDeleted == false &&
                                    q.tbStudent.tbSysUser.Id == Code.Common.UserId
                                    select p.tbElectiveOrg.Id).ToList();

                var tb = (from p in db.Table <Entity.tbElectiveOrg>()
                          where p.tbElective.Id == vm.ElectiveId &&
                          p.tbCourse.IsDeleted == false &&
                          p.tbElectiveGroup.IsDeleted == false &&
                          p.tbElectiveSection.IsDeleted == false &&
                          (p.IsPermitClass == false || limitOrgList.Contains(p.Id))
                          orderby p.tbElectiveGroup.Id
                          select new
                {
                    //p.Id,
                    //p.OrgName,
                    //p.MaxCount,
                    //p.RemainCount,
                    //CourseId = p.tbCourse.Id,
                    //p.tbCourse.CourseName,
                    //p.tbTeacher.TeacherName,
                    //RoomName = p.tbRoom.RoomName,
                    ElectiveSectionId = p.tbElectiveSection.Id,
                    ElectiveSectionNo = p.tbElectiveSection.No,
                    ElectiveSectionName = p.tbElectiveSection.ElectiveSectionName,
                    ElectiveSectionMinElective = p.tbElectiveSection.MinElective,
                    ElectiveSectionMaxElective = p.tbElectiveSection.MaxElective,
                    ElectiveGroupId = p.tbElectiveGroup.Id,
                    ElectiveGroupNo = p.tbElectiveGroup.No,
                    ElectiveGroupName = p.tbElectiveGroup.ElectiveGroupName,
                    ElectiveGroupMinElective = p.tbElectiveGroup.MinElective,
                    ElectiveGroupMaxElective = p.tbElectiveGroup.MaxElective,
                }).ToList();

                vm.ElectiveSectionList = (from p in tb
                                          group p by new { p.ElectiveSectionId, p.ElectiveSectionNo, p.ElectiveSectionName, p.ElectiveSectionMaxElective, p.ElectiveSectionMinElective } into g
                                          select new Dto.ElectiveSection.Info
                {
                    Id = g.Key.ElectiveSectionId,
                    No = g.Key.ElectiveSectionNo,
                    ElectiveSectionName = g.Key.ElectiveSectionName,
                    MaxElective = g.Key.ElectiveSectionMaxElective,
                    MinElective = g.Key.ElectiveSectionMinElective
                }).OrderBy(d => d.No).ToList();

                vm.ElectiveGroupList = (from p in tb
                                        group p by new { p.ElectiveGroupId, p.ElectiveGroupNo, p.ElectiveGroupName, p.ElectiveGroupMaxElective, p.ElectiveGroupMinElective } into g
                                        select new Dto.ElectiveGroup.Info
                {
                    Id = g.Key.ElectiveGroupId,
                    No = g.Key.ElectiveGroupNo,
                    ElectiveGroupName = g.Key.ElectiveGroupName,
                    MaxElective = g.Key.ElectiveGroupMaxElective,
                    MinElective = g.Key.ElectiveGroupMinElective
                }).OrderBy(d => d.No).ToList();

                var electiveDataList = (from p in db.Table <Entity.tbElectiveData>()
                                        where p.tbElectiveOrg.tbElective.Id == vm.ElectiveId &&
                                        p.tbStudent.tbSysUser.Id == Code.Common.UserId
                                        select new
                {
                    p.tbElectiveOrg.Id,
                    p.IsFixed,
                    p.tbElectiveOrg.OrgName,
                    GroupId = p.tbElectiveOrg.tbElectiveGroup.Id,
                    SectionId = p.tbElectiveOrg.tbElectiveSection.Id,
                    Permit = p.tbElectiveOrg.Permit,
                    p.tbElectiveOrg.tbTeacher.TeacherName,
                    p.tbElectiveOrg.tbRoom.RoomName,
                    p.tbElectiveOrg.RemainCount
                }).ToList();

                //已选列表
                vm.ElectiveOrgList = (from p in electiveDataList
                                      select new Dto.ElectiveOrg.List()
                {
                    OrgName = p.OrgName,
                    Id = p.Id,
                    IsFixed = p.IsFixed,
                    IsChecked = true,
                    ElectiveGroupId = p.GroupId,
                    ElectiveSectionId = p.SectionId,
                    Permit = p.Permit,
                    TeacherName = p.TeacherName,
                    RoomName = p.RoomName,
                    RemainCount = p.RemainCount
                }).ToList();

                var orgStudent = (from p in db.Table <Entity.tbElectiveOrgStudent>()
                                  where p.tbStudent.tbSysUser.Id == Code.Common.UserId
                                  select p.tbElectiveOrg.Id).ToList();
                //白名单
                vm.ElectiveOrgList.RemoveAll(d => d.Permit == 1 && orgStudent.Contains(d.Id) == false);
                //黑名单
                vm.ElectiveOrgList.RemoveAll(d => d.Permit == -1 && orgStudent.Contains(d.Id));


                vm.IsHiddenSection = vm.ElectiveOrgList.Select(d => d.ElectiveSectionName).Distinct().Count() <= 1;
                vm.IsHiddenGroup   = vm.ElectiveOrgList.Select(d => d.ElectiveGroupName).Distinct().Count() <= 1;

                return(View(vm));
            }
        }