Пример #1
0
        public ActionResult Edit(int id = 0)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.TeacherDept.Edit();
                vm.TeacherDeptParentList = SelectList();

                if (id != 0)
                {
                    var tb = (from p in db.Table <Teacher.Entity.tbTeacherDept>()
                              where p.Id == id
                              select new Dto.TeacherDept.Edit
                    {
                        Id = p.Id,
                        No = p.No,
                        TeacherDeptName = p.TeacherDeptName,
                        TeacherDeptParentId = p.tbTeacherDeptParent.Id,
                        TeacherDeptParentName = p.tbTeacherDeptParent.TeacherDeptName
                    }).FirstOrDefault();
                    if (tb != null)
                    {
                        vm.TeacherDeptEdit = tb;
                    }
                }

                return(View(vm));
            }
        }
Пример #2
0
        public ActionResult Edit(Models.TeacherDept.Edit vm)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var error = new List <string>();
                if (error.Count == decimal.Zero)
                {
                    if (db.Table <Teacher.Entity.tbTeacherDept>().Where(d => d.TeacherDeptName == vm.TeacherDeptEdit.TeacherDeptName && d.Id != vm.TeacherDeptEdit.Id).Any())
                    {
                        error.AddError("部门名称已存在!");
                    }
                    else
                    {
                        if (vm.TeacherDeptEdit.Id == 0)
                        {
                            var tb = new Teacher.Entity.tbTeacherDept();
                            tb.No = vm.TeacherDeptEdit.No == null?db.Table <Teacher.Entity.tbTeacherDept>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.TeacherDeptEdit.No;

                            tb.TeacherDeptName     = vm.TeacherDeptEdit.TeacherDeptName;
                            tb.tbTeacherDeptParent = db.Set <Teacher.Entity.tbTeacherDept>().Find(vm.TeacherDeptEdit.TeacherDeptParentId);
                            db.Set <Teacher.Entity.tbTeacherDept>().Add(tb);
                            if (db.SaveChanges() > 0)
                            {
                                XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加部门");
                            }
                        }
                        else
                        {
                            var tb = (from p in db.Table <Teacher.Entity.tbTeacherDept>()
                                      where p.Id == vm.TeacherDeptEdit.Id
                                      select p).FirstOrDefault();

                            if (tb != null)
                            {
                                if (tb.Id == vm.TeacherDeptEdit.TeacherDeptParentId)
                                {
                                    error.AddError("不能选择自己作为父节点!");
                                    return(Code.MvcHelper.Post(error));
                                }


                                if (vm.TeacherDeptEdit.TeacherDeptParentId.HasValue)
                                {
                                    var sunList = GetSon(db, tb.Id);
                                    if (sunList.ToList().Contains(vm.TeacherDeptEdit.TeacherDeptParentId.Value))
                                    {
                                        error.AddError("不能选择自己的子节点作为父节点!");
                                        return(Code.MvcHelper.Post(error));
                                    }
                                    else
                                    {
                                        tb.tbTeacherDeptParent = db.Set <Teacher.Entity.tbTeacherDept>().Find(vm.TeacherDeptEdit.TeacherDeptParentId);
                                    }
                                }
                                else
                                {
                                    //删除父级
                                    db.Entry <Entity.tbTeacherDept>(tb).Reference(p => p.tbTeacherDeptParent).Load();
                                    tb.tbTeacherDeptParent = null;
                                }

                                tb.No = vm.TeacherDeptEdit.No == null?db.Table <Teacher.Entity.tbTeacherDept>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1 : (int)vm.TeacherDeptEdit.No;

                                tb.TeacherDeptName = vm.TeacherDeptEdit.TeacherDeptName;


                                if (db.SaveChanges() > 0)
                                {
                                    XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("修改部门");
                                }
                            }
                            else
                            {
                                error.AddError(Resources.LocalizedText.MsgNotFound);
                            }
                        }
                    }
                }

                return(Code.MvcHelper.Post(error));
            }
        }