0

I'm trying to upload a file using Ajax.BeginForm() in ASP.NET MVC 5.2.

View:

@model EventsImages
@using (Ajax.BeginForm("Create", "EventsImages", new AjaxOptions { HttpMethod = "POST", OnComplete = "completeForm(xhr, status,'listDialog1')" }, new { enctype = "multipart/form-data", @class = "form", @role = "form" }))
    {
       <div class="col-md-12 col-sm-12">
          @Html.AntiForgeryToken()
          ...
          <input type="file" id="upload" name="upload" />
          <input type="submit" value="Create" class="btn" />

Controller:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(EventsImages eventsImages, HttpPostedFileBase upload)
{
    if (ModelState.IsValid)
    { 

The problem is that HttpPostedFileBase upload is always null.

I know this problem is mentioned several times but I couldn't find a solution, or a workaround.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    You cannot upload files with the AjaxHelper. Here is how to upload files with ajax https://stackoverflow.com/questions/2428296/jquery-ajax-upload-file-in-asp-net-mvc. – Jasen Feb 26 '19 at 18:13
  • 1
    While I believe Jasen is correct, the link is a non-mvc way to upload files. Look for another answer on SO. The following is more inline with the MVC way: https://stackoverflow.com/a/38703844/209259. – Erik Philips Feb 26 '19 at 18:15

0 Answers0