Be sure that you use asp.net core 3.x.Due to the github issue,asp.net core 2.2 could not receive the IFormFile which in the Model in the server side.
Model:
public class FileInfo
{
public string FileName { get; set; }
public string InputName { get; set; }
public IFormFile File { get; set; }
}
View:
@model FileInfo
<div class="row">
<div class="col-md-4">
<form asp-action="Create" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="FileName" class="control-label"></label>
<input asp-for="FileName" class="form-control" />
<span asp-validation-for="FileName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="InputName" class="control-label"></label>
<input asp-for="InputName" class="form-control" />
<span asp-validation-for="InputName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="File" class="control-label"></label>
<input asp-for="File" value="Upload">
<span asp-validation-for="File" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Controller:
public class FileInfoController : Controller
{
public IActionResult Create()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create(FileInfo fileInfo)
{
if (ModelState.IsValid)
{
//save file info..
}
return View(fileInfo);
}
}
UPDATE:
For asp.net core 2.2,if your IFormFile
is in a nested model,a working demo like below:
Model:
public class FileInfo
{
public int Id { get; set; }
public string FileName { get; set; }
public string InputName { get; set; }
public IFormFile File { get; set; }
}
public class Test
{
public int Id { get; set; }
public string Name { get; set; }
public FileInfo FileInfo { get; set; }
}
View:
@model Test
<div class="row">
<div class="col-md-4">
<form asp-action="Create" enctype="multipart/form-data">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FileInfo.FileName" class="control-label"></label>
<input asp-for="FileInfo.FileName" class="form-control" />
<span asp-validation-for="FileInfo.FileName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FileInfo.InputName" class="control-label"></label>
<input asp-for="FileInfo.InputName" class="form-control" />
<span asp-validation-for="FileInfo.InputName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="FileInfo.File" class="control-label"></label>
<input asp-for="FileInfo.File" value="Upload">
<span asp-validation-for="FileInfo.File" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
Controller:
[HttpPost]
public async Task<IActionResult> Create(Test test)
{
if (ModelState.IsValid)
{
//save file info..
}
return View(test);
}
Result:
