[英]Why does ModelState.IsValid always return false when trying to upload an image in ASP.NET?
我一直尝试在 asp.net 核心中上传图像,但无论我怎么做,我都会遇到同样的问题。 ModelState.IsValid
返回 false。 我最终屈服了,只是复制并粘贴了别人的作品,希望如果我能让它发挥作用,即使它是别人的,我也能明白我哪里出错了。 即使使用我的脚本小子技术,我的计划也被挫败了。 所以现在我遇到了比以往任何时候都更聪明的人。
这是我复制的教程
我的 model 看起来像这样:
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
public class ImageModel
{
[Key]
public int ImageId { get; set; }
[Column(TypeName = "nvarchar(50)")]
public string Title { get; set; }
[Column(TypeName = "nvarchar(100)")]
[DisplayName("Image Name")]
public string ImageName { get; set; }
[NotMapped]
[DisplayName("Upload File")]
public IFormFile ImageFile { get; set; }
}
我的相关部分controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("ImageId,Title,ImageFile")] ImageModel imageModel)
{
var errors = ModelState.Values.SelectMany(v => v.Errors);
if (ModelState.IsValid)
{
// Save image to wwwroot/image
string wwwRootPath = _hostEnvironment.WebRootPath;
string fileName = Path.GetFileNameWithoutExtension(imageModel.ImageFile.FileName);
string extension = Path.GetExtension(imageModel.ImageFile.FileName);
imageModel.ImageName = fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension;
string path = Path.Combine(wwwRootPath + "/Image/", fileName);
using (var fileStream = new FileStream(path, FileMode.Create))
{
await imageModel.ImageFile.CopyToAsync(fileStream);
}
// Insert record
_context.Add(imageModel);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
return View(imageModel);
}
最后是视图
@model ImageModel
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>ImageModel</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Title" class="control-label"></label>
<input asp-for="Title" class="form-control" />
<span asp-validation-for="Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="ImageFile" class="control-label"></label>
<input asp-for="ImageFile" accept="image/*" />
<span asp-validation-for="ImageFile" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>
@section Scripts {
@{
await Html.RenderPartialAsync("_ValidationScriptsPartial");
}
}
我已经看过这个问题并遵循了建议。 它返回的错误是
ModelError、ModelError.Exception、ModelError.ErrorMessage Microsoft.AspNetCore.Mvc.ModelBinding.ModelError
图片名称字段是必需的
感谢您的时间
ModelState 不查看[Bind]
中的属性,而是查看 model 的所有属性。
新建视图 model 不带ImageName
属性或设置ImageName
默认值
public class ImagePostModel
{
[Key]
public int ImageId { get; set; }
[Column(TypeName = "nvarchar(50)")]
public string Title { get; set; }
//[Column(TypeName = "nvarchar(100)")]
//[DisplayName("Image Name")]
//public string ImageName { get; set; } = String.Empty Alternative solution
[NotMapped]
[DisplayName("Upload File")]
public IFormFile ImageFile { get; set; }
}
该问题是由可空引用类型引起的,请检查您的 csproj 文件:
<PropertyGroup>
.......
<Nullable>enable</Nullable>
......
</PropertyGroup>
如果包含以下代码,请尝试将其设置为禁用
有多种不同的解决方案
解决方案1:在你的startup/program.cs中设置如下:
builder.Services.AddControllersWithViews(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true);
有关更多详细信息,您可以查看此文档
解决方案2:
在您不想验证的属性上添加[ValidateNever]
属性
解决方案3:
创建一个 ViewModel 不包含视图的 ImageName 属性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.