【问题标题】:IFormFile in model is always null模型中的 IFormFile 始终为空
【发布时间】:2019-12-17 09:51:14
【问题描述】:

我有一个要提交的文件上传表单。下面是 ViewModel 和它的 DataAnnotations。提交表单后,ModelState 变得错误。在 ViewModel 中检查 File 属性时,它为空。尽管我保留了enctype="multipart/form-data,但我仍然为空。

谁能帮帮我。

public class ExcelUploadViewModel
    {

        /// <summary>
        /// Gets or Sets the FileName
        /// </summary>
        [Required(ErrorMessage = "FileName is required")]
        public string FileName { get; set; }

        [Required(ErrorMessage = "File is required")]
        [DataType(DataType.Upload)]
        public IFormFile File { get; set; }
   }

Controller.cs

[HttpPost]
    public async Task<IActionResult> UploadExcel(ExcelUploadViewModel excelUploadModel)
    {
        if (ModelState.IsValid)
        {
            // HttpResponseMessage response;
            TransactionResultBase transactionResultBase = new TransactionResultBase();
            IFormFile file = Request.Form.Files[0];
        }
    }

还有FormUpload.cshtml

<div align="left">
            <form id="uploadForm" enctype="multipart/form-data" name="uploadForm" asp-action="UploadExcel" method="post" >

                <div class="form-group form-group-lg form-group-sm row " >
                    <div class="col-sm-12 col-md-10 col-lg-10 uploadDiv" style="display: flex !important">
                        <label asp-for="FileName" class="col-sm-12 col-md-10 col-lg-10" style="font-size: 15px; max-width: fit-content ">File Name :</label>
                        <input asp-for="FileName" class="form form-control fileName"
                               type="text"
                               name="fileName"
                               placeholder="Enter your file name"
                               id="fileName" />
                        <span asp-validation-for="FileName" class="text-danger"></span>
                        <input asp-for="File" required class="form-control file" type="file" placeholder="File Name" id="file" name="uploadFile" />
                    </div>
                </div>
                <small>Please upload .xls or .xlxs or json or xml formatted files only</small>
                <div class="form-group form-group-lg form-group-sm row">
                    <div class="col-sm-12 col-md-10 col-lg-10">
                        <input type="submit" class="btn btn-primary" name="submit" id="fileUploadButton" value="Upload" />
                        <input type="reset" class="btn btn-Gray" name="result" id="resetButton" value="Reset" />
                    </div>
                </div>
            </form>
        </div>

【问题讨论】:

    标签: .net-core asp.net-core-2.1 asp.net-core-2.2


    【解决方案1】:

    标签助手根据模型生成 id 和 name 属性。所以不要同时使用标签助手和名称属性。

    如果你想使用标签助手 asp-for 就足够了。如果你想使用“名称”属性,那么你应该根据你的模型和属性名称来使用它。在这种情况下,由于您将名称属性用于文件输入name="uploadFile",您的模型无法确定是哪个属性,因此您应该更正它name="File"

    如果您在视图中使用视图模型,请注意它会发生变化。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      • 2017-02-15
      • 2019-01-25
      • 1970-01-01
      相关资源
      最近更新 更多