I have an ASP.NET MVC app attempting to upload an IFormFile
. The uploaded file is accessible in the controller but after redirecting from the controller.
I need to display the previously attached file and other form errors if there are any.
How can we display or hold the attached file if there is a model error.
Controller:
public IActionResult AddEmployee(Employee model)
{
// Do validation for Employee model
if (!ModelState.IsValid)
{
return View("Add", model);
}
return View("View");
}
.cshtml
view:
<form method="post" encType="multipart/form-data">
<!-- /.row -->
<div class="row">
<div class="col-sm-5 col-md-6">
<div class="form-group">
<label> Attachment</label>
<input Name="Attachment" id="Attachment" Type="file" accept=".doc,.docx,.xls,.xlsx,.ppt,.pptx,.jpg,.jpeg,.png,.bmp,.tiff,.pdf,.msg,.zip" style="font-weight:normal;">
</div>
</div>
</div>
<!-- /.col-lg-12 -->
</form>
Model class:
public class Employee
{
public IFormFile Attachment { get; set; }
}