Home > Software engineering >  File not being passed from view to controller - ASP.NET Core
File not being passed from view to controller - ASP.NET Core

Time:04-09

After clicking the submit button, the attachment list object is always null at the controller. Here's the code:

View model:

public class CreateTicketViewModel
{
    ...
    public List<IFormFile> Attachments { get; set; }

}

View:

<form asp-action="CreateTicket">
...
     <div >
          <label asp-for="Attachments" >Attachments</label>
          <input asp-for="Attachments"  multiple />
     </div>
     <div >
          <input type="submit" value="Create"  />
     </div>
</form>

Post method declaration:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateTicket([FromForm]CreateTicketViewModel createTicketViewModel)

All in .NET 5

CodePudding user response:

remove [ValidateAntiForgeryToken] and [FromForm]

[HttpPost]
public async Task<IActionResult> CreateTicket(CreateTicketViewModel createTicketViewModel)

CodePudding user response:

I got it to work. The form tag has to have enctype attribute with a value of "multipart/formdata".

  • Related