如何使用ASP.NET Core MVC中的验证属性来验证IFormFile属性

时间:2019-05-03 16:33:29

标签: c# asp.net validation asp.net-core

我正在使用ASP.NET Core MVC,并希望通过验证属性来验证模型中的IFormFile属性。

我的模特:

[Required(ErrorMessage = "Please upload an book image")]
public IFormFile BookImage { get; set; }

我的cshtml文件:

<input asp-for="BookImage" type="file" accept="image/*"/>
<span asp-validation-for="BookImage" class="text-danger"></span>

但是当不选择文件并按提交按钮时,表单仍然可以提交。它不需要BookImage。

为什么?以及如何使IFormFile成为必需?谢谢。

0 个答案:

没有答案