public ActionResult Edit(int id = 0)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.ElectiveGroup.Edit();

                if (id != 0)
                {
                    var tb = (from p in db.Table <Entity.tbElectiveGroup>()
                              where p.Id == id
                              select p).FirstOrDefault();
                    if (tb != null)
                    {
                        vm.ElectiveGroupEdit = tb;
                    }
                }

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

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

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

                            tb.tbElective        = db.Set <Entity.tbElective>().Find(vm.ElectiveId);
                            tb.ElectiveGroupName = vm.ElectiveGroupEdit.ElectiveGroupName;
                            tb.MaxElective       = vm.ElectiveGroupEdit.MaxElective;
                            tb.MinElective       = vm.ElectiveGroupEdit.MinElective;
                            db.Set <Entity.tbElectiveGroup>().Add(tb);

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

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

                                tb.ElectiveGroupName = vm.ElectiveGroupEdit.ElectiveGroupName;
                                tb.MaxElective       = vm.ElectiveGroupEdit.MaxElective;
                                tb.MinElective       = vm.ElectiveGroupEdit.MinElective;

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

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