Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net core Can';在ASP.NET核心MVC中更新映像_Asp.net Core - Fatal编程技术网

Asp.net core Can';在ASP.NET核心MVC中更新映像

Asp.net core Can';在ASP.NET核心MVC中更新映像,asp.net-core,Asp.net Core,我是ASP.NET核心的初学者,我正在尝试更新数据库中的图像,我对我的代码有一些疑问,图像添加到数据库中但无法更新 AdminController public async Task<ViewResult> Edit(int productId) { return View(await _repository.Products .FirstOrDefaultAsync(p => p.ProductID ==

我是ASP.NET核心的初学者,我正在尝试更新数据库中的图像,我对我的代码有一些疑问,图像添加到数据库中但无法更新

AdminController

public async Task<ViewResult> Edit(int productId)
        {
            return View(await _repository.Products
                .FirstOrDefaultAsync(p => p.ProductID == productId));
        }

        [HttpPost]
        public async Task<IActionResult> Edit(Product product, IFormFile image)
        {
            if (ModelState.IsValid)
            {
                if(image != null && image.Length > 0)
                {
                    var fileName = Path.GetFileName(image.FileName);
                    var filePath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot\\images\\items", fileName);
                    using(var fileSteam = new FileStream(filePath, FileMode.Create))
                    {
                        await image.CopyToAsync(fileSteam);
                    }
                    product.Image = fileName;
                }
                _repository.SaveProduct(product);
                TempData["message"] = $"{product.Name} has been edit";
                return RedirectToAction("Index");
            }
            else
            {
                return View(product);
            }
        }
公共异步任务编辑(int-productId)
{
返回视图(等待_repository.Products
.FirstOrDefaultAsync(p=>p.ProductID==ProductID));
}
[HttpPost]
公共异步任务编辑(产品、文件映像)
{
if(ModelState.IsValid)
{
if(image!=null&&image.Length>0)
{
var fileName=Path.GetFileName(image.fileName);
var filePath=Path.Combine(Directory.GetCurrentDirectory(),“wwwroot\\images\\items”,文件名);
使用(var fileSteam=newfilestream(filePath,FileMode.Create))
{
等待image.CopyToAsync(fileSteam);
}
product.Image=文件名;
}
_存储库。保存产品(产品);
TempData[“message”]=$“{product.Name}已被编辑”;
返回操作(“索引”);
}
其他的
{
返回视图(产品);
}
}
Edit.cshtml

<form asp-action="Edit" method="post" enctype="multipart/form-data">

    <input type="hidden" asp-for="ProductID" />

    <div class="form-group">
         <h4><label asp-for="Image"></label></h4>
         <input asp-for="Image" type="file" class="form-control" />
    </div>

我错在哪里了,谢谢邹星!我更新了
SaveProduct(产品)


错误是什么?我的应用程序可以在数据库中添加和删除图像,但无法更新。我不知道为什么你说不更新是什么意思?似乎您将文件名保存在数据库中,并将文件保存在
wwwroot
文件夹中,
Image
列是否未被替换为新文件名?我尝试了您的代码,它已成功更新。由于您未发布回复代码,我使用
\u context.Update(产品);wait_context.SaveChangesAsync()就可以了。请确保
product.ProductID
正确无误。
public Product SaveProduct(Product productDataUpdate)
        {
            var prod = _context.Products.Attach(productDataUpdate);
            prod.State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            _context.SaveChanges();
            return productDataUpdate;
        }