public ActionResult Edit(int id = 0) { using (var db = new XkSystem.Models.DbContext()) { var vm = new Models.Period.Edit(); vm.PeriodTypeList = PeriodTypeController.SelectList(); if (id != 0) { var tb = (from p in db.Table <Basis.Entity.tbPeriod>() where p.Id == id select new Dto.Period.Edit { Id = p.Id, No = p.No, PeriodTypeId = p.tbPeriodType.Id, PeriodName = p.PeriodName }).FirstOrDefault(); if (tb != null) { vm.PeriodEdit = tb; } } return(View(vm)); } }
public ActionResult Edit(Models.Period.Edit vm) { using (var db = new XkSystem.Models.DbContext()) { var error = new List <string>(); if (error.Count == decimal.Zero) { if (db.Table <Basis.Entity.tbPeriod>().Where(d => d.PeriodName == vm.PeriodEdit.PeriodName && d.Id != vm.PeriodEdit.Id).Any()) { error.AddError("该节次已存在!"); } else { if (vm.PeriodEdit.Id == 0) { var tb = new Basis.Entity.tbPeriod(); tb.No = vm.PeriodEdit.No == null?db.Table <Basis.Entity.tbPeriod>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.PeriodEdit.No; tb.PeriodName = vm.PeriodEdit.PeriodName; tb.tbPeriodType = db.Set <Entity.tbPeriodType>().Find(vm.PeriodEdit.PeriodTypeId); db.Set <Basis.Entity.tbPeriod>().Add(tb); if (db.SaveChanges() > 0) { XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加了节次"); } } else { var tb = (from p in db.Table <Basis.Entity.tbPeriod>() where p.Id == vm.PeriodEdit.Id select p).FirstOrDefault(); if (tb != null) { tb.No = vm.PeriodEdit.No == null?db.Table <Basis.Entity.tbPeriod>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.PeriodEdit.No; tb.PeriodName = vm.PeriodEdit.PeriodName; tb.tbPeriodType = db.Set <Entity.tbPeriodType>().Find(vm.PeriodEdit.PeriodTypeId); if (db.SaveChanges() > 0) { XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改了节次"); } } else { error.AddError(Resources.LocalizedText.MsgNotFound); } } } } return(Code.MvcHelper.Post(error)); } }