public static string InsertDefault(XkSystem.Models.DbContext db, int electiveId)
        {
            var tb = new Entity.tbElectiveSection();

            tb.No                  = 1;
            tb.tbElective          = db.Set <Entity.tbElective>().Find(electiveId);
            tb.ElectiveSectionName = "默认分段";
            tb.MaxElective         = 99;
            tb.MinElective         = 0;
            db.Set <Entity.tbElectiveSection>().Add(tb);

            if (db.SaveChanges() > 0)
            {
                XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加了选课时段");
            }

            return(string.Empty);
        }
        public ActionResult Edit(Models.ElectiveSection.Edit vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var error = new List <string>();

                if (vm.ElectiveSectionEdit.MaxElective < vm.ElectiveSectionEdit.MinElective)
                {
                    error.AddError("最大选课数和最小选课数设置错误,无法比较大小!");
                }

                if (error.Count == decimal.Zero)
                {
                    if (db.Table <Entity.tbElectiveSection>().Where(d => d.ElectiveSectionName == vm.ElectiveSectionEdit.ElectiveSectionName && d.tbElective.Id == vm.ElectiveId && d.Id != vm.ElectiveSectionEdit.Id).Any())
                    {
                        error.AddError("该选课分段已存在!");
                    }
                    else
                    {
                        if (vm.ElectiveSectionEdit.Id == 0)
                        {
                            var tb = new Entity.tbElectiveSection();
                            tb.No = vm.ElectiveSectionEdit.No == null?db.Table <Entity.tbElectiveSection>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.ElectiveSectionEdit.No;

                            tb.tbElective          = db.Set <Entity.tbElective>().Find(vm.ElectiveId);
                            tb.ElectiveSectionName = vm.ElectiveSectionEdit.ElectiveSectionName;
                            tb.MaxElective         = vm.ElectiveSectionEdit.MaxElective;
                            tb.MinElective         = vm.ElectiveSectionEdit.MinElective;
                            db.Set <Entity.tbElectiveSection>().Add(tb);

                            if (db.SaveChanges() > 0)
                            {
                                XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加了选课时段");
                            }
                        }
                        else
                        {
                            var tb = (from p in db.Table <Entity.tbElectiveSection>()
                                      where p.Id == vm.ElectiveSectionEdit.Id
                                      select p).FirstOrDefault();

                            if (tb != null)
                            {
                                tb.No = vm.ElectiveSectionEdit.No == null?db.Table <Entity.tbElectiveSection>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.ElectiveSectionEdit.No;

                                tb.ElectiveSectionName = vm.ElectiveSectionEdit.ElectiveSectionName;
                                tb.MaxElective         = vm.ElectiveSectionEdit.MaxElective;
                                tb.MinElective         = vm.ElectiveSectionEdit.MinElective;

                                if (db.SaveChanges() > 0)
                                {
                                    XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改了选课时段");
                                }
                            }
                            else
                            {
                                error.AddError(Resources.LocalizedText.MsgNotFound);
                            }
                        }
                    }
                }

                return(Code.MvcHelper.Post(error));
            }
        }