ASP.NET MVC Razor - 上传文件,HttpPostedFileBase始终为null

Jac*_*8PD 2 asp.net-mvc enctype httppostedfilebase razor jquery-mobile

嗯,这很奇怪.我正在做我在网上大量发现的一切,但仍然无效.我想上传一个文件,所以在View I中:

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

这是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");
      ...
   }
}
Run Code Online (Sandbox Code Playgroud)

它总是打印Inside Import + null.所以我确定我调用了正确的Controller Action,但它总是收到一个null HttpPostedFileBase.

有没有人有建议或已经找到(并解决)这种问题?谢谢!

Jac*_*8PD 13

发现了问题.你必须在视图中放置html属性"@data_ajax ="false"",在enctype 1附近.所以,如果您使用的是jQuery Mobile,请务必写下来

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

希望这会对某人有所帮助!