コード例 #1
0
        public ActionResult EditNews(News EditedNews, IFormFile UploadImg, string deletingImgPath)
        {
            if (ModelState.IsValid)
            {
                if (UploadImg != null && UploadImg.Length > 0)
                {
                    // deleting old image from its path :
                    if (System.IO.File.Exists(deletingImgPath))
                    {
                        System.IO.File.Delete(deletingImgPath);
                    }

                    string ImgPath = Path.Combine(env.WebRootPath + "\\imageUploads\\NewsImg", UploadImg.FileName);
                    UploadImg.CopyTo(new FileStream(ImgPath, FileMode.Create));


                    EditedNews.Image = UploadImg.FileName;

                    bool successfullyEditingNews = DAL.EditExistedNews(EditedNews.ID, EditedNews);
                    if (successfullyEditingNews)
                    {
                        return(RedirectToAction("GetAllNews"));
                    }
                    else
                    {
                        return(View(EditedNews));
                    }
                }
                else
                {
                    ViewBag.Message = "You have not specified a file yet ...";
                }
            }
            return(View(EditedNews));
        }
コード例 #2
0
ファイル: NewsController.cs プロジェクト: akg2011-code/Smart
        public ActionResult EditNews(News EditedNews, HttpPostedFileBase UploadImg, string deletingImgPath)
        {
            if (ModelState.IsValid)
            {
                if (UploadImg != null && UploadImg.ContentLength > 0)
                {
                    // deleting old image from its path :
                    if (System.IO.File.Exists(deletingImgPath))
                    {
                        System.IO.File.Delete(deletingImgPath);
                    }

                    string ImgPath = Path.Combine(Server.MapPath("~/imageUploads/NewsImg"), UploadImg.FileName);
                    UploadImg.SaveAs(ImgPath);


                    EditedNews.Image = UploadImg.FileName;

                    bool successfullyEditingNews = DAL.EditExistedNews(EditedNews.ID, EditedNews);
                    if (successfullyEditingNews)
                    {
                        return(RedirectToAction("GetAllNews"));
                    }
                    else
                    {
                        return(View(EditedNews));
                    }
                }
                else
                {
                    ViewBag.Message = "You have not specified a file yet ...";
                }
            }
            return(View(EditedNews));
        }