1

我已经在这个问题上敲了将近 2 天了。我将不胜感激任何帮助。无论我做什么,我的 IFormFile 始终为空。这是 MVC 设置:

剃刀视图:

<form method="post" enctype="multipart/form-data" asp-controller="Admin" asp-action="LoadProvidersBulk">
    <h6 class="text-muted">Upload Provider File</h6>
    <h6 class="text-muted small">Supported File Types: <b>.xlsx</b></h6>

    <div class="form-group-row d-inline-flex badge-dark" style="padding-top:10px; padding-bottom:10px;">
        <div class="col-sm-4" style="padding:2px">
            <input type="file" asp-for="FormFile" name="ProviderUpload" class="form-control-sm" />
        </div>
    </div>
    <div class="form-group row" style="padding-top:10px">
        <div class="col-sm-5 text-left">
            <button class="btn btn-sm btn-dark btn-primary" type="submit">Load Providers</button>
            <a asp-action="LoadProviders" class="btn btn-sm btn-dark btn-primary">Cancel</a>
        </div>
    </div>
</form>

模型:

using SampleProject.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SampleProject.ViewModels
{
    public class LoadProvidersBulkViewModel
    {
        public IFormFile FormFile { get; set; }
    }
}

控制器:

        [HttpPost]
        public async Task<IActionResult> LoadProvidersBulk([FromForm(Name = "ProviderUpload")] LoadProvidersBulkViewModel loadProvidersBulkViewModel, CancellationToken cancellationToken)
        {
            var file = loadProvidersBulkViewModel.FormFile;            

            if (file == null || file.Length <= 0)
            {
                ModelState.AddModelError("BulkUploadError", "The file cannot be empty");
                return View("LoadProvidersBulk");
            }

            /* Removed all other code for brevity */

            return RedirectToAction("Users", _userManager.Users);
        }
4

3 回答 3

3

只需更改您的操作参数,如下所示:

  public async Task<IActionResult> LoadProvidersBulk([FromForm] LoadProvidersBulkViewModel loadProvidersBulkViewModel, CancellationToken cancellationToken)

并且您的输入名称必须更改为FormFile

<input type="file" asp-for="FormFile" name="FormFile" class="form-control-sm" />
于 2020-03-09T04:58:36.187 回答
1

您混淆asp-for 并命名属性nameasp-for="FormFile" name="ProviderUpload")。

Input Tag Helper 将 HTML<input>元素绑定到剃刀视图中的模型表达式是 -

<input asp-for="<Expression Name>">

为 asp-for 属性中指定的表达式名称生成 id 和 name HTML 属性。asp-for="Property1.Property2"相当于m => m.Property1.Property2。表达式的名称用于asp-for属性值。

所以你的代码看起来像 -

看法:-

@model SampleProject.ViewModels.LoadProvidersBulkViewModel


<form method="post" enctype="multipart/form-data" asp-controller="Admin" asp-action="LoadProvidersBulk">
    <h6 class="text-muted">Upload Provider File</h6>
    <h6 class="text-muted small">Supported File Types: <b>.xlsx</b></h6>

    <div class="form-group-row d-inline-flex badge-dark" style="padding-top:10px; padding-bottom:10px;">
        <div class="col-sm-4" style="padding:2px">
            <input type="file" asp-for="FormFile" class="form-control-sm" />
        </div>
    </div>
    <div class="form-group row" style="padding-top:10px">
        <div class="col-sm-5 text-left">
            <button class="btn btn-sm btn-dark btn-primary" type="submit">Load Providers</button>
            <a asp-action="LoadProviders" class="btn btn-sm btn-dark btn-primary">Cancel</a>
        </div>
    </div>
</form>

控制器:-

    [HttpPost]
    public async Task<IActionResult> LoadProvidersBulk(LoadProvidersBulkViewModel model)
    {
        var file = model.FormFile;            

        if (file == null || file.Length <= 0)
        {
            ModelState.AddModelError("BulkUploadError", "The file cannot be empty");
            return View("LoadProvidersBulk");
        }

        /* Removed all other code for brevity */

        return RedirectToAction("Users", _userManager.Users);
    }
于 2020-03-09T04:43:36.843 回答
0

我发现了问题。我为 IFormFile 实现了自己的接口类,而不是使用 Microsoft.AspNetCore.Http 指令。从模型中删除该引用后,它按预期工作。

感谢那些花时间回复的人。

于 2020-03-09T15:02:49.263 回答