我正在使用名爲「jquery.form.js」(http://malsup.com/jquery/form/#getting-started)的jquery插件能夠在我的ASP中使用Ajax上載文件。網絡MVC項目。我遇到的問題是,當我按下表單中的提交按鈕時,處理上傳的控制器操作被調用兩次。第一次被稱爲HttpPostedFileBase對象,正確設置了上傳的文件。第二次將其稱爲HttpPostedFileBase對象設置爲Nothing。我不希望處理程序被調用兩次。我怎樣才能防止雙重職位?使用jquery.form.js jquery插件導致我的表單被提交兩次
的觀點:
@Using Html.BeginForm("Document", "NewDocument", Model, FormMethod.Post, New With {.enctype = "multipart/form-data", .id = "submitimage-form"})
<input type="file" id="fileInput" name="fileInput" />
<input type="submit" id="submitbutton" value="Accept" data-mini="true" data-theme="b" data-icon="check" />
End Using
<script src="~/Scripts/jquery.form.js"></script>
<script>
$(document).on("pageshow", '#choosenewdocument', function() {
$("#submitimage-form").ajaxForm(function() {
});
});
</script>
的控制器處理器:
<HttpPost()> _
Function Document(ByVal model As MaxDocument, fileInput As HttpPostedFileBase) As ActionResult
...
End Function
我想你的解決方案,但我得到的錯誤:JavaScript運行時錯誤:對象不支持屬性或方法「的preventDefault」。 –
表單提交後,您是否呈現相同的頁面?如果是這樣,它可能會在'pageshow'上提交你的表單。 – WannaCSharp
我剛剛嘗試過使用'pageshow',它確實發佈了我的表單兩次。你爲什麼要在'pageshow'上提交你的表單? – WannaCSharp