Ejemplo n.º 1
0
        [HttpGet]                                                //读取数据库数据
        public IActionResult Edit(int id)                        //通过Edit操作方法找到Edit视图
        {
            Student student = _studentRepository.GetStudent(id); //查询是否在数据库里
            StudentEditVideModel studentEditVidew = new StudentEditVideModel {
                Id                = student.Id,
                Name              = student.Name,
                Email             = student.Email,
                ClassName         = student.ClassName,
                ExistingPhotoPath = student.PhotoPath
            };

            return(View(studentEditVidew));
        }
Ejemplo n.º 2
0
        public IActionResult Edit(StudentEditVideModel model)  //创建StudentEditVideModel用于接收post表单数据
        {
            if (ModelState.IsValid)
            {
                Student student = _studentRepository.GetStudent(model.Id);
                student.Email     = model.Email;//视图模型赋值给领域模型
                student.Name      = model.Name;
                student.ClassName = model.ClassName;
                if (model.Photos != null && model.Photos.Count > 0)  //用户有无选择新头像
                {
                    if (model.ExistingPhotoPath != null)
                    {
                        string filePath = Path.Combine(hostingEnvironment.WebRootPath, "images", model.ExistingPhotoPath);
                        System.IO.File.Delete(filePath);//删除数据库旧头像  //Delete没有继承所以不是这里的问题,问题在FileStream流处理
                    }
                    student.PhotoPath = ProcessUploadedFile(model);
                }

                Student updataStudent = _studentRepository.Update(student);
                return(RedirectToAction("Index"));
            }
            return(View(model));
        }