public ActionResult Edit(int id = 0)
        {
            if (Code.Common.UserType != Code.EnumHelper.SysUserType.Administrator && Code.Common.UserType != Code.EnumHelper.SysUserType.Teacher)
            {
                return(Content(Code.Common.Redirect(Url.Action("Index", "SysIndex", new { area = "Sys" }), "当前身份类别无法访问该功能!")));
            }

            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.Elective.Edit();
                vm.ElectiveTypeList = Areas.Elective.Controllers.ElectiveTypeController.SelectList();
                vm.YearList         = Basis.Controllers.YearController.SelectList(Code.EnumHelper.YearType.Section);

                if (vm.ElectiveTypeList.Count > 0)
                {
                    vm.ElectiveEdit.ElectiveTypeId = vm.ElectiveTypeList.FirstOrDefault().Value.ConvertToInt();
                }

                if (id != 0)
                {
                    var tb = (from p in db.Table <Entity.tbElective>()
                              where p.Id == id
                              select new Dto.Elective.Edit
                    {
                        Id = p.Id,
                        No = p.No,
                        ElectiveName = p.ElectiveName,
                        ElectiveTypeId = p.tbElectiveType.Id,
                        IsPop = p.IsPop,
                        FromDate = p.FromDate,
                        ToDate = p.ToDate,
                        IsDisable = p.IsDisable,
                        Remark = p.Remark
                    }).FirstOrDefault();
                    if (tb != null)
                    {
                        vm.ElectiveEdit  = tb;
                        vm.FromDate      = tb.FromDate.ToString(Code.Common.StringToDateTime);
                        vm.ToDate        = tb.ToDate.ToString(Code.Common.StringToDateTime);
                        vm.ApplyFromDate = tb.ApplyFromDate.ToString(Code.Common.StringToDateTime);
                        vm.ApplyToDate   = tb.ApplyToDate.ToString(Code.Common.StringToDateTime);
                    }
                }

                return(View(vm));
            }
        }
        public ActionResult Edit(Models.Elective.Edit vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                int electiveId = 0;

                var error = new List <string>();
                if (error.Count == decimal.Zero)
                {
                    var isExists = db.Table <Entity.tbElective>().Count(p => p.ElectiveName == vm.ElectiveEdit.ElectiveName && p.Id != vm.ElectiveEdit.Id) > 0;
                    if (isExists)
                    {
                        error.AddError("系统中已存在相同名字的选课记录!");
                    }
                    else
                    {
                        if (vm.ElectiveEdit.Id == 0)
                        {
                            var tb = new Entity.tbElective();
                            tb.No = vm.ElectiveEdit.No == null?db.Table <Entity.tbElective>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.ElectiveEdit.No;

                            tb.ElectiveName   = vm.ElectiveEdit.ElectiveName;
                            tb.tbElectiveType = db.Set <Entity.tbElectiveType>().Find(vm.ElectiveEdit.ElectiveTypeId);
                            tb.IsPop          = vm.ElectiveEdit.IsPop;
                            tb.IsDisable      = vm.ElectiveEdit.IsDisable;
                            tb.Remark         = vm.ElectiveEdit.Remark;
                            tb.FromDate       = DateTime.Parse(vm.FromDate);
                            tb.ToDate         = DateTime.Parse(vm.ToDate);
                            tb.ApplyFromDate  = DateTime.Parse(vm.ApplyFromDate);
                            tb.ApplyToDate    = DateTime.Parse(vm.ApplyToDate);

                            db.Set <Entity.tbElective>().Add(tb);
                            if (db.SaveChanges() > 0)
                            {
                                XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加了选课设置");
                                ElectiveSectionController.InsertDefault(db, tb.Id);
                                ElectiveGroupController.InsertDefault(db, tb.Id);
                            }

                            electiveId = tb.Id;
                        }
                        else
                        {
                            var tb = (from p in db.Table <Entity.tbElective>()
                                      where p.Id == vm.ElectiveEdit.Id
                                      select p).FirstOrDefault();

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

                                tb.ElectiveName   = vm.ElectiveEdit.ElectiveName;
                                tb.tbElectiveType = db.Set <Entity.tbElectiveType>().Find(vm.ElectiveEdit.ElectiveTypeId);
                                tb.IsPop          = vm.ElectiveEdit.IsPop;
                                tb.FromDate       = DateTime.Parse(vm.FromDate);
                                tb.ToDate         = DateTime.Parse(vm.ToDate);
                                tb.ApplyFromDate  = DateTime.Parse(vm.ApplyFromDate);
                                tb.ApplyToDate    = DateTime.Parse(vm.ApplyToDate);
                                tb.IsDisable      = vm.ElectiveEdit.IsDisable;
                                tb.Remark         = vm.ElectiveEdit.Remark;
                                if (db.SaveChanges() > 0)
                                {
                                    XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改了选课设置");
                                }
                            }
                            else
                            {
                                error.AddError(Resources.LocalizedText.MsgNotFound);
                            }
                        }
                    }
                }

                if (Request["Step"] != null)
                {
                    return(Code.MvcHelper.Post(error, Url.Action("List", "ElectiveClass", new { ElectiveId = electiveId })));
                }
                else
                {
                    return(Code.MvcHelper.Post(error, Url.Action("List")));
                }
            }
        }