public ActionResult Edit(int id = 0) { using (var db = new XkSystem.Models.DbContext()) { var vm = new Models.SurveyOption.Edit(); if (id != 0) { var tb = (from p in db.Table <Entity.tbSurveyOption>() where p.Id == id select new Dto.SurveyOption.Edit { Id = p.Id, No = p.No, OptionName = p.OptionName, OptionValue = p.OptionValue //SurveyItemId = p.tbSurveyItem.Id }).FirstOrDefault(); if (tb != null) { vm.OptionEdit = tb; } } return(View(vm)); } }
public ActionResult Edit(Models.SurveyOption.Edit vm) { using (var db = new XkSystem.Models.DbContext()) { var error = new List <string>(); if (error.Count == decimal.Zero) { if (vm.OptionEdit.Id == 0) { var tb = new Entity.tbSurveyOption(); tb.No = vm.OptionEdit.No == null?db.Table <Entity.tbSurveyOption>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.OptionEdit.No; tb.tbSurveyItem = db.Set <Entity.tbSurveyItem>().Find(vm.SurveyItemId); tb.OptionName = vm.OptionEdit.OptionName; tb.OptionValue = vm.OptionEdit.OptionValue; db.Set <Entity.tbSurveyOption>().Add(tb); if (db.SaveChanges() > 0) { XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加评价选项"); } } else { var tb = (from p in db.Table <Entity.tbSurveyOption>() where p.Id == vm.OptionEdit.Id select p).FirstOrDefault(); if (tb != null) { tb.No = vm.OptionEdit.No == null?db.Table <Entity.tbSurveyOption>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.OptionEdit.No; tb.tbSurveyItem = db.Set <Entity.tbSurveyItem>().Find(vm.SurveyItemId); tb.OptionName = vm.OptionEdit.OptionName; tb.OptionValue = vm.OptionEdit.OptionValue; if (db.SaveChanges() > 0) { XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改评价选项"); } } else { error.AddError(Resources.LocalizedText.MsgNotFound); } } } return(Code.MvcHelper.Post(error)); } }