Search code examples
razorasp.net-core-3.1iformfile

.net core IFormFile null when uploading file from view


I'm trying to bind a file to an IFormFile attribute in my model. When I upload the file the attribute is remains null. Any idea what I'm doing wrong?

My code looks something like this:

Model:

public class myModel
{
  public IFormFile UploadedFile {get; set; }
}

View:

@model myModel

 // binding other attributes
@Html.TextBoxFor(model => model.UploadedFile, null, new { @type = "file", @class = "input-file", @style = "small_box", @enctype = "multipart/form-data" })
 // binding other attributes

For some reason, when the attribute type is a string, it manages to populate UploadedFile with the file name, however when it's an IFormFile it remains null (while the other attributes are successfully populated).


Solution

  • I was using an Html.BeginForm and I guess the form itself needed the "multipart/form-data" enctype so adding new { enctype = "multipart/form-data" } to the form solved my issue.