【发布时间】:2022-11-04 04:37:37
【问题描述】:
我是mvc的程序员, 我的目标是使用我在数据库中的图像并将其编辑为另一个不一定存在于我的 wwwroot 但在我的计算机中的图像。 图片:
图片说明:我这里的数据库中有一张图片,我想编辑它 点击编辑按钮 编辑图片:
但是当我按下保存按钮时,我得到一个错误:
NullReferenceException:Object reference not set to an instance of an object. PetShop.Client.Services.FileService.File(CreateAnimalViewModel model) in FileService.cs var path = Path.Combine(wwwPath, "Images", model.Photo!.FileName); PetShop.Client.Controllers.AdminController.EditAnimal(CreateAnimalViewModel model) in AdminController.cs await _file.File(model);
必须注意,当我尝试将新图像添加到 wwwroot 但在编辑中不起作用时,服务的代码确实有效
我的服务:
public class FileService : IFileService
{
private readonly IWebHostEnvironment _environment;
public FileService(IWebHostEnvironment environment)
{
_environment = environment;
}
public async Task<string> File([FromForm] CreateAnimalViewModel model)
{
string wwwPath = _environment.WebRootPath;
var path = Path.Combine(wwwPath, "Images", model.Photo!.FileName);
if (model.Photo.Length > 0)
{
using var stream = new FileStream(path, FileMode.Create);
await model.Photo.CopyToAsync(stream);
}
return model.Animal!.PhotoUrl = model.Photo.FileName;
}
}
public interface IFileService
{
Task<string> File([FromForm] CreateAnimalViewModel model);
}
我的视图模型:
public class CreateAnimalViewModel
{
public Animal? Animal { get; set; }
public IFormFile Photo { get; set; }
}
我的控制器:
public async Task<IActionResult> EditAnimal(int id)
{
var animal = await _repo.FindAnimalById(id);
ViewBag.Category = new SelectList(_repository.GetCategoriesTable(), "CategoryId", "Name");
return View(new CreateAnimalViewModel() { Animal = animal});
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditAnimal([FromForm] CreateAnimalViewModel model)
{
ModelState.Clear();
TryValidateModel(model);
await _file.File(model);
if (!ModelState.IsValid)
{
await _repo.EditAnimal(model.Animal!);
return RedirectToAction(nameof(Manager));
}
return View();
}
我的观点:
@model PetShop.Client.Models.CreateAnimalViewModel
<div >
<form asp-action="EditAnimal" method="post" >
<div asp-validation-summary="ModelOnly"></div><input type="hidden" asp-for="Animal!.AnimalId" id="Space"/>
<dl class="row" >
<dt class = "col-sm-2"><label asp-for="Animal!.Name" id="Space"></label></dt>
<dd class = "col-sm-10"><input asp-for="Animal!.Name"/><span asp-validation-for="Animal!.Name" ></span></dd>
<dt class = "col-sm-2"><label asp-for="Animal!.BirthDate" id="Space"></label></dt>
<dd class = "col-sm-10"><input asp-for="Animal!.BirthDate"/><span asp-validation-for="Animal!.BirthDate"></span></dd>
<dt class = "col-sm-2"><label asp-for="Animal!.Description" id="Space"></label></dt>
<dd class = "col-sm-10"><input asp-for="Animal!.Description"/><span asp-validation-for="Animal!.Description"></span></dd>
<dt class = "col-sm-2"><label asp-for="Animal!.CategoryId" id="Space"></label></dt>
<dd class = "col-sm-10"><select asp-for="Animal!.CategoryId" asp-items="ViewBag.Category"></select><span asp-validation-for="Animal!.CategoryId"></span></dd>
<dt class = "col-sm-2"><label asp-for="Photo"></label></dt>
<dd class = "col-sm-10"><input type="file" asp-for="Photo" accept="image/*"/>
<span asp-validation-for="Photo"></span></dd>
<br/> <br/> <br/>
<input type="submit" value="Save" id="ButtonDesign"/>
</dl>
</form>
<a asp-action="Commands"><input type="submit" value="Back to Admin Page" id="BackPageButton"/></a>
鉴于我只显示文件的一部分,所有其他的东西都与问题无关
编辑帖子存储库:
public async Task<int> AddAnimal(Animal animal)
{
_context.Add(animal!);
return await _context.SaveChangesAsync();
}
public async Task<int> EditAnimal(Animal animal)
{
_context.Update(animal);
return await _context.SaveChangesAsync();
}
public DbSet<Category> GetCategories()
{
var category = _context.Categories;
return category;
}
【问题讨论】:
-
您还没有提供数据库信息。还提供有助于重现加载此页面的代码,以便可以从这里开始
-
这个概念是当你加载这个页面时,首先需要像这样检查现有数据库信息的图像路径,
string checkFile = Path.GetFullPath(Path.Combine(_hostEnvironment.WebRootPath, "FolderName", "ImageNameMapInDatabase.png"));这里你必须检查它是否存在,否则你会得到exception -
如果您可以检查
checkFile是否为空,那么您不应该遇到异常,在File方法的代码中设置条件path不为空或为空 -
男人,编辑意味着您要替换现有映像,或者如果不存在新的图像,则首先将其加载到
Id,然后使用Id or Name将要修改,然后上传新图片,然后替换为由Id您拥有的。 -
最好将您的项目上传到某处并共享链接,以便可以直接检查它,因为您当前的代码不完整,无法模拟。
标签: c# asp.net-core asp.net-core-mvc