public ActionResult Edit(Models.Exam.Edit vm) { using (var db = new XkSystem.Models.DbContext()) { var error = new List <string>(); if (error.Count == decimal.Zero) { if (vm.ExamEdit.Id == 0) { var tb = new Exam.Entity.tbExam(); tb.No = vm.ExamEdit.No == null?db.Table <Exam.Entity.tbExam>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.ExamEdit.No; tb.ExamName = vm.ExamEdit.ExamName; tb.IsPublish = vm.ExamEdit.IsPublish; tb.tbYear = db.Set <Basis.Entity.tbYear>().Find(vm.ExamEdit.YearId); tb.tbExamType = db.Set <Exam.Entity.tbExamType>().Find(vm.ExamEdit.ExamTypeId); tb.tbExamLevelGroup = db.Set <Exam.Entity.tbExamLevelGroup>().Find(vm.ExamEdit.LevelGroupId); tb.tbExamSegmentGroup = db.Set <Exam.Entity.tbExamSegmentGroup>().Find(vm.ExamEdit.SegmentGroupId); db.Set <Exam.Entity.tbExam>().Add(tb); if (db.SaveChanges() > 0) { XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加考试"); } } else { var tb = (from p in db.Table <Exam.Entity.tbExam>().Include(d => d.tbExamLevelGroup).Include(d => d.tbExamSegmentGroup) where p.Id == vm.ExamEdit.Id select p).FirstOrDefault(); if (tb != null) { tb.No = vm.ExamEdit.No == null?db.Table <Exam.Entity.tbExam>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.ExamEdit.No; tb.ExamName = vm.ExamEdit.ExamName; tb.IsPublish = vm.ExamEdit.IsPublish; tb.tbYear = db.Set <Basis.Entity.tbYear>().Find(vm.ExamEdit.YearId); tb.tbExamType = db.Set <Exam.Entity.tbExamType>().Find(vm.ExamEdit.ExamTypeId); tb.tbExamLevelGroup = db.Set <Exam.Entity.tbExamLevelGroup>().Find(vm.ExamEdit.LevelGroupId); tb.tbExamSegmentGroup = db.Set <Exam.Entity.tbExamSegmentGroup>().Find(vm.ExamEdit.SegmentGroupId); if (db.SaveChanges() > 0) { XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改考试"); } } else { error.AddError(Resources.LocalizedText.MsgNotFound); } } } return(Code.MvcHelper.Post(error)); } }
public ActionResult Edit(int id = 0) { using (var db = new XkSystem.Models.DbContext()) { var vm = new Models.Exam.Edit(); vm.YearList = Basis.Controllers.YearController.SelectList(Code.EnumHelper.YearType.Section); if (vm.YearList.Count > 0 && vm.ExamEdit.YearId == 0) { vm.ExamEdit.YearId = vm.YearList.OrderByDescending(d => d.Selected).FirstOrDefault().Value.ConvertToInt(); } vm.ExamTypeList = Areas.Exam.Controllers.ExamTypeController.SelectList(); vm.LevelGroupList = Areas.Exam.Controllers.ExamLevelGroupController.SelectTotalList(); vm.SegmentGroupList = Areas.Exam.Controllers.ExamSegmentGroupController.SelectList(); if (id != 0) { var tb = (from p in db.Table <Exam.Entity.tbExam>().Include(d => d.tbExamLevelGroup).Include(d => d.tbExamSegmentGroup) where p.Id == id select new Dto.Exam.Edit { Id = p.Id, No = p.No, ExamName = p.ExamName, IsPublish = p.IsPublish, YearId = p.tbYear.Id, ExamTypeId = p.tbExamType.Id, LevelGroupId = p.tbExamLevelGroup.Id, SegmentGroupId = p.tbExamSegmentGroup.Id }).FirstOrDefault(); if (tb != null) { vm.ExamEdit = tb; } } return(View(vm)); } }