AspNet Core-[必需]属性不适用于文件输入 [英] AspNet Core - [Required] attribute not working on file input
本文介绍了AspNet Core-[必需]属性不适用于文件输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有简单视图的AspNet Core Web应用程序,允许用户上传文件.视图模型中的file属性具有[Required]属性.但是,即使没有选择文件,单击提交"按钮也将执行发布.
I have an AspNet Core web app with a simple view to allow the user to upload a file. The file property in the view model has a [Required] attribute. However, clicking the submit button will perform a post even if there is no file selected.
查看模型:
public class DocumentUploadViewModel
{
[HiddenInput]
public string Id { get; set; }
[Required]
[FileExtensions(Extensions = "pdf")]
public IFormFile Document { get; set; }
}
查看:
@using App.Models.DocumentViewModels
@model DocumentUploadViewModel
@{
ViewData["Title"] = "Upload Document";
}
<h2>Upload Document</h2>
<form method="post" enctype="multipart/form-data" asp-controller="Document" asp-action="Upload" >
<div class="form-horizontal">
<div class="form-group">
<input asp-for="Id" />
<label class="col-md-2 control-label">Document</label>
<div class="col-md-10">
<input class="form-control" type="file" asp-for="Document" />
<span asp-validation-for="Document" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input id="save" name="save" type="submit" value="Upload" class="btn btn-default pull-right" />
</div>
</div>
</div>
</form>
推荐答案
查看全文