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.

2
  • 2
    You cannot upload files with the AjaxHelper. Here is how to upload files with ajax stackoverflow.com/questions/2428296/…. Commented Feb 26, 2019 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: stackoverflow.com/a/38703844/209259. Commented Feb 26, 2019 at 18:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.