Пример #1
0
        public async Task <IActionResult> EditNews(NewsImageTagsEditViewModel newsViewModel, string tagList)
        {
            if (string.IsNullOrEmpty(tagList))
            {
                TempData["Error"] = "خبر حداقل باید یک برچسب داشته باشد";
                return(RedirectToAction("EditNews", new { id = newsViewModel.News.Id }));
            }

            newsViewModel.News.Tags = tagList;

            if (!ModelState.IsValid)
            {
                return(View(newsViewModel));
            }

            if (newsViewModel.File != null && newsViewModel.File.Length != 0)
            {
                var    oldImage     = newsViewModel.News.ImageName;
                string oldImagePath = Path.Combine(Directory.GetCurrentDirectory(),
                                                   "wwwroot/Administrators/assets/images/news/", oldImage);
                if (System.IO.File.Exists(oldImagePath))
                {
                    System.IO.File.Delete(oldImagePath);
                }
                else
                {
                    TempData["Error"] = "خطا در حذف عکس قبلی";
                    return(RedirectToAction("News"));
                }

                newsViewModel.News.ImageName = Guid.NewGuid() + Path.GetExtension(newsViewModel.File.FileName);
                string savePath = Path.Combine(
                    Directory.GetCurrentDirectory(), "wwwroot/Administrators/assets/images/news",
                    newsViewModel.News.ImageName
                    );
                await using var stream = new FileStream(savePath, FileMode.Create);
                await newsViewModel.File.CopyToAsync(stream);
            }

            _db.News.Update(newsViewModel.News);
            await _db.SaveChangesAsync();

            TempData["Success"] = "خبر با موفقیت ویرایش شد";
            return(RedirectToAction("News"));
        }
Пример #2
0
        public async Task <IActionResult> EditNews(int id = 0)
        {
            if (id == 0)
            {
                return(NotFound());
            }

            var news = await _db.News.FindAsync(id);

            if (news == null)
            {
                return(NotFound());
            }

            var newsViewModel = new NewsImageTagsEditViewModel()
            {
                News = news
            };

            return(View(newsViewModel));
        }