2013-10-16 16 views
0

我正在使用名爲「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 

回答

0

嘗試preventDefault

<script> 
$(document).on("pageshow", '#choosenewdocument', function() { 
     $("#submitimage-form").ajaxForm({ 
     beforeSubmit: function() { 
      return false; 
     } 
     }); 

    }); 
</script> 

preventDefault(顧名思義)阻止默認動作的發生。

+0

我想你的解決方案,但我得到的錯誤:JavaScript運行時錯誤:對象不支持屬性或方法「的preventDefault」。 –

+0

表單提交後,您是否呈現相同的頁面?如果是這樣,它可能會在'pageshow'上提交你的表單。 – WannaCSharp

+0

我剛剛嘗試過使用'pageshow',它確實發佈了我的表單兩次。你爲什麼要在'pageshow'上提交你的表單? – WannaCSharp

0

因此,防止重複提交時使用jQuery AJAX,只是把這個在您的形式

action="javascript:null();"

+0

當我把它放在我的表單中並提交表單時,我得到一個javascript運行時錯誤:在jquery 1.7.1中,訪問被拒絕。 –