public async Task <IActionResult> EditNews(News news, List <IFormFile> files) { if (ModelState.IsValid) { News item = await _dbContext.News.Include(p => p.Images).SingleOrDefaultAsync(p => p.Id == news.Id); //修改新闻信息 if (item != null) { if (files.Count() > 0) { news.Images = new List <NewsImage>(); foreach (var f in files) { news.Images.Add(new NewsImage { Url = f.FileName, NewsId = news.Id, MimeType = f.ContentType }); } } //上传的图片文件夹路径 string upPath = Path.Combine(_env.WebRootPath, "images"); //原图片路径 List <string> paths = new List <string>(); foreach (string i in await GetImageUrl(item.Id)) { paths.Add(Path.Combine(_env.WebRootPath, i)); } //删除原图片并上传新图片 MyFile.Remove(paths); MyFile.Up(upPath, files).Wait(); //保存修改后的新闻信息 item.CopyFrom(news); await _dbContext.SaveChangesAsync(); TempData["Message"] = "编辑成功!"; return(RedirectToAction("Index")); } } return(View(news)); }