asp.net-mvcjquery-mobilerazorhttppostedfilebaseenctype

ASP.NET MVC Razor - Uploading File, HttpPostedFileBase always null


Well, this is weird. I'm doing everything as I massively found on the web, but still doesn't work. I want to upload a file, so in the View I put:

@using (Html.BeginForm("Import", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<input type="file" name="uploadFile" />
<input type="submit" value="Importa" />
}

and this is Home/Import:

[AcceptVerbs(HttpVerbs.Post)] // or [HttpPost], nothing changes
public ActionResult Import(HttpPostedFileBase uploadFile)
{
   Debug.WriteLine("Inside Import");
   if (uploadFile == null)
       Debug.WriteLine("null");
   // Verify that the user selected a file
   if (uploadFile != null && uploadFile.ContentLength > 0)
   {
      Debug.WriteLine("Valid File");
      ...
   }
}

and it always prints Inside Import + null. So I'm sure I call the right Controller Action but it always receive a null HttpPostedFileBase.

Does anyone have advice for it or have already found (and solved) this kind of problem? Thanks!


Solution

  • Found the problem. You have to put the html attribute "@data_ajax = "false" " in the view, near the enctype one. So, if you are using jQuery Mobile, be sure to write

    @using (Html.BeginForm("Import", "Home", FormMethod.Post, new { enctype = "multipart/form-data", @data_ajax = "false" }))
    

    Hope this will help someone!