使用Ajax.BeginForm iso Html.BeginForm的Null参数

时间:2013-10-09 09:16:38

标签: ajax asp.net-mvc asp.net-mvc-4

有一个处理文件上传的控制器方法:

AcceptVerbs(HttpVerbs.Post)]
public ActionResult FileUpload(int id, HttpPostedFileBase uploadFile)
{
    if (uploadFile != null && uploadFile.ContentLength > 0 && ...)
    ...
}

当我使用Html.BeginForm时,一切正常。用Ajax.BeginForm替换Html.BeginForm会导致uploadFile为null值(上述方法的第二个参数):

<div id="ajaxDocumentUpload">
@{ using (Ajax.BeginForm("FileUpload", "ProjectDocument", FormMethod.Post, 
           new AjaxOptions { OnSuccess = "UploadSuccess" }, 
           new { enctype = "multipart/form-data" }))
  {
    <div>
        <input type=file accept="image/gif, image/jpeg" name="uploadFile">     
    </div>
    <div>
        <input type="hidden" name="id" id="id" value="@Model.ProjectId"/>
    </div>

    <input id=btnUpdateAttachment type=submit value="Upload">
  } 
}
</div>

知道出了什么问题吗?在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

原来是一个安全问题,请查看Upload file using ajax call MVC3以获得答案。

答案 1 :(得分:0)

您将不兼容的参数传递给Ajax.BeginForm()。请改用:

@using(Ajax.BeginForm("FileUpload", "ProjectDocument", null, 
    new AjaxOptions { OnSuccess = "UploadSuccess" }, 
    new { enctype = "multipart/form-data" })

MSDN documentation