示例#1
0
        public ActionResult Edit(int id = 0)
        {
            using (var db = new XkSystem.Models.DbContext())
            {
                var vm = new Models.TeacherHonor.Edit();
                vm.TeacherHonorLevelList = TeacherHonorLevelController.SelectList();
                vm.TeacherHonorTypeList  = TeacherHonorTypeController.SelectList();

                if (id > 0)
                {
                    vm.DataEdit = (from p in db.Table <Teacher.Entity.tbTeacherHonor>()
                                   where p.Id == id
                                   select new Dto.TeacherHonor.Edit()
                    {
                        Id = p.Id,
                        No = p.No,
                        TeacherId = p.tbTeacher.Id,
                        TeacherCode = p.tbTeacher.TeacherCode,
                        TeacherName = p.tbTeacher.TeacherName,
                        HonorName = p.HonorName,
                        HonorFile = p.HonorFile,
                        TeacherHonorLevelId = p.tbTeacherHonorLevel.Id,
                        TeacherHonorTypeId = p.tbTeacherHonorType.Id
                    }).FirstOrDefault();
                }

                return(View(vm));
            }
        }
示例#2
0
        public ActionResult Edit(Models.TeacherHonor.Edit vm)
        {
            var error = new List <string>();

            if (error.Count == decimal.Zero)
            {
                using (var db = new XkSystem.Models.DbContext())
                {
                    var fileName = string.Empty;
                    var file     = Request.Files["DataEdit.HonorFile"];
                    if (Code.Common.GetFileType(file.FileName) != Code.FileType.Image)
                    {
                        return(Content("<script >alert('图片格式必须是jpg、jpeg、png、bmp格式!');history.go(-1);</script >", "text/html"));
                    }
                    if (string.IsNullOrEmpty(file.FileName) == false)
                    {
                        fileName = Guid.NewGuid().ToString() + System.IO.Path.GetExtension(file.FileName);
                        var fileSave = Server.MapPath("~/Files/TeacherHonor/");
                        file.SaveAs(fileSave + fileName);
                    }


                    if (vm.DataEdit.Id > 0)
                    {
                        var tb = db.Set <Teacher.Entity.tbTeacherHonor>().Find(vm.DataEdit.Id);
                        tb.HonorFile           = fileName;
                        tb.HonorName           = vm.DataEdit.HonorName;
                        tb.No                  = vm.DataEdit.No > 0 ? (int)vm.DataEdit.No : db.Table <Teacher.Entity.tbTeacherHonor>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1;
                        tb.tbTeacher           = db.Set <Teacher.Entity.tbTeacher>().Find(vm.DataEdit.TeacherId);
                        tb.tbTeacherHonorLevel = db.Set <Teacher.Entity.tbTeacherHonorLevel>().Find(vm.DataEdit.TeacherHonorLevelId);
                        tb.tbTeacherHonorType  = db.Set <Teacher.Entity.tbTeacherHonorType>().Find(vm.DataEdit.TeacherHonorTypeId);
                    }
                    else
                    {
                        if (db.Table <Teacher.Entity.tbTeacherHonor>().Where(d => d.HonorName == vm.DataEdit.HonorName).Any())
                        {
                            return(Content("<script >alert('该教师已有相同荣誉!');window.parent.location.reload();</script >", "text/html"));
                        }
                        var teacherCode = vm.DataEdit.TeacherNameCode.Split('(')[0];
                        var tb          = new Teacher.Entity.tbTeacherHonor()
                        {
                            HonorFile           = fileName,
                            HonorName           = vm.DataEdit.HonorName,
                            No                  = vm.DataEdit.No > 0 ? (int)vm.DataEdit.No : db.Table <Teacher.Entity.tbTeacherHonor>().Select(d => d.No).DefaultIfEmpty(0).Max() + 1,
                            tbTeacher           = db.Table <Teacher.Entity.tbTeacher>().Where(d => d.TeacherCode == teacherCode).FirstOrDefault(),
                            tbTeacherHonorLevel = db.Set <Teacher.Entity.tbTeacherHonorLevel>().Find(vm.DataEdit.TeacherHonorLevelId),
                            tbTeacherHonorType  = db.Set <Teacher.Entity.tbTeacherHonorType>().Find(vm.DataEdit.TeacherHonorTypeId)
                        };
                        db.Set <Teacher.Entity.tbTeacherHonor>().Add(tb);
                    }

                    if (db.SaveChanges() > 0)
                    {
                        XkSystem.Areas.Sys.Controllers.SysUserLogController.Insert("添加/修改教师荣誉");
                    }
                }
            }

            return(Content("<script >window.parent.location.reload();</script >", "text/html"));
        }