コード例 #1
0
ファイル: ClassController.cs プロジェクト: turkgulsun/Blog
        public ActionResult Edit(int id)
        {
            try
            {
                var         query       = _classService.GetById(id);
                ClassEditVM classEditVM = _mapper.Map <ClassEditVM>(query);
                return(View(classEditVM));
            }
            catch (Exception)
            {
                return(View());

                throw;
            }
        }
コード例 #2
0
ファイル: ClassController.cs プロジェクト: turkgulsun/Blog
        public ActionResult Edit(ClassEditVM classEditVM, IFormFile uploadfile)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    var model = new ClassEditVM();
                    return(View(model));
                }


                //Yeni image var mı?
                string combineFileName = null;
                if (uploadfile != null)
                {
                    //Önceki resmi silelim.
                    if (System.IO.File.Exists("Uploads/Class/" + classEditVM.Image))
                    {
                        System.IO.File.Delete("Uploads/Class/" + classEditVM.Image);
                    }

                    string fileName = Guid.NewGuid().ToString();
                    string fileType = System.IO.Path.GetExtension(uploadfile.FileName);
                    combineFileName   = fileName + fileType;
                    classEditVM.Image = combineFileName;

                    using (var localFile = System.IO.File.OpenWrite("Uploads/Class/" + combineFileName))
                        using (var uploadedFile = uploadfile.OpenReadStream())
                        {
                            uploadedFile.CopyTo(localFile);
                        }
                }

                ClassDTO classDTO = _mapper.Map <ClassDTO>(classEditVM);
                _classService.Update(classDTO);

                TempData["Statu"] = "ok";
                return(RedirectToAction("index"));
            }
            catch (Exception)
            {
                return(View());

                throw;
            }
        }