public ActionResult Edit(int id = 0) { using (var db = new XkSystem.Models.DbContext()) { var vm = new Models.ElectiveData.Edit(); if (id != 0) { var tb = (from p in db.Table <Entity.tbElectiveData>() where p.Id == id select new Dto.ElectiveData.Edit { Id = p.Id, ElectiveOrgId = p.tbElectiveOrg.Id, StudentId = p.tbStudent.Id, IsFixed = p.IsFixed, IsPreElective = p.IsPreElective }).FirstOrDefault(); if (tb != null) { vm.ElectiveDataEdit = tb; } } return(View(vm)); } }
public ActionResult Edit(Models.ElectiveData.Edit vm) { using (var db = new XkSystem.Models.DbContext()) { var error = new List <string>(); if (error.Count == decimal.Zero) { if (vm.ElectiveDataEdit.Id == 0) { var tb = new Entity.tbElectiveData() { IsFixed = vm.ElectiveDataEdit.IsFixed, IsPreElective = vm.ElectiveDataEdit.IsPreElective, tbElectiveOrg = db.Set <Entity.tbElectiveOrg>().Find(vm.ElectiveDataEdit.ElectiveOrgId), tbStudent = db.Set <Student.Entity.tbStudent>().Find(Code.Common.UserId), }; db.Set <Entity.tbElectiveData>().Add(tb); if (db.SaveChanges() > 0) { XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加了选课数据"); } } else { //var tb = (from p in db.Table<Entity.tbElectiveData>() // where p.Id == vm.ElectiveDataEdit.Id // select p).FirstOrDefault(); //if (tb != null) //{ // if (db.SaveChanges() > 0) // { // XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改了选课数据"); // } //} //else //{ // error.AddError(Resources.LocalizedText.MsgNotFound); //} } } return(Code.MvcHelper.Post(error)); } }