现在我已经在这上面敲了将近2天了。我将不胜感激任何帮助。无论我做什么,我的IFormFile始终为null。这是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);
}
答案 0 :(得分:1)
只需如下更改操作参数:
public async Task<IActionResult> LoadProvidersBulk([FromForm] LoadProvidersBulkViewModel loadProvidersBulkViewModel, CancellationToken cancellationToken)
并且您的输入名称必须更改为 FormFile :
<input type="file" asp-for="FormFile" name="FormFile" class="form-control-sm" />
答案 1 :(得分:0)
您混用了asp-for
和名称属性name
(asp-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);
}
答案 2 :(得分:0)
我发现了问题。我为IFormFile实现了自己的接口类,而不是使用Microsoft.AspNetCore.Http指令。一旦从模型中删除该引用,它就会按预期工作。
感谢您抽出宝贵的时间来答复。