2013-05-21 34 views
0

我在我的項目中有一個upload.ascx。它將在Jquery Popup中加載。防止MVC表單使用Javascript提交功能

upload.ascx包含File Upload對照。文件上傳控件將上傳.xlsx and .xls文件。我已經添加了Java script函數來做這個驗證(防止不必要的文件上傳)。

Onchange of FileUpload控制和Onclick of Submit button相同的驗證功能將被調用。

文件上傳驗證功能正在爲兩種呼叫工作。如果用戶點擊提交按鈕,相同的功能被調用並且工作正常。

我的問題是:在我的驗證功能,我寫了return false。顯示警報消息後,頁面將被重定向到某個URL(localhost/Admin/Authorization/AcceptFile.aspx)。 AcceptFile是我的ActionResult名稱。它不應該發生。函數返回False。但表單被重定向到URL以上爲什麼。?

如果錯誤的文件(它是正確的),我在我的操作結果中保留了調試器。如果其正確的文件索引文件將被加載成功消息(動作結果正在調用並工作正常)。 。

我懷疑MVC表格。如果錯誤的文件被上傳,重定向發生在沒有調用Action Result的情況下,應該停止。

<% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data" })) 
     {%> 

我的JavaScript函數:

function checkfile(sender) { 
     var validExts = new Array(".xlsx", ".xls"); 
     var fileExt = sender.value; 
     var strValue = false; 
     fileExt = fileExt.substring(fileExt.lastIndexOf('.')); 
     if (validExts.indexOf(fileExt) < 0) { 
      ShowMessage(-1, "Invalid file selected, valid files are .xlsx and .xls types."); 
      strValue = false; 
     } 
     else { 
      strValue = true; 
     } 
     return strValue; 
    } 

我upload.ascx代碼:

<div> 
    <% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data" })) 
     {%> 

      <input type="file" id="fileAuthorization" name="fileAuthorization" onchange="checkfile(this);" /> 

      <input type="submit" id="btnSave" name="btnSave" value="Upload" onclick="javascript:return checkfile(document.getElementById('fileAuthorization'))" /> 

    <%} %> 
</div> 

回答

4

我自己我發現這個問題。

是的我的嫌疑人是正確的。問題是

onsubmit = "javascript:return checkfile(document.getElementById('fileAuthorization'))" 

本應在表標籤本身被稱爲像這樣:

<% using (Html.BeginForm("AcceptFile", "Authorization", FormMethod.Post, new { enctype = "multipart/form-data", onsubmit = "javascript:return checkfile(document.getElementById('fileAuthorization'))" })) 
     {%> 

但此前它被稱爲Submit按鈕的onclick

<input type="submit" id="btnSave" name="btnSave" value="Upload" onclick="javascript:return checkfile(document.getElementById('fileAuthorization'))" /> 

我已經糾正其得到工作(驗證和文件上傳提交按鈕onclick和文件上傳控制onchange)。

但我的問題是爲什麼return false不提交按鈕點擊?但它的工作Fileupload onchange事件。

爲什麼發生了重定向。在將相同的Line更改爲MVC Form之後,它的工作(重定向現在不再發生)爲什麼。?

+0

只需gr8 !!它解決了我的問題,,,謝謝花花公子 (刪除onclick從輸入類型的按鈕,並添加在@ Html.BeginForm onsubmit) –

+0

很高興聽到它的幫助...它正確的我解釋說,它是我的早先的代碼。 :)謝謝老兄。:) – RajeshKdev

+0

謝謝你。這對我幫助很大。 –

0

更改提交至:

<button type="button" id="btnSave" name="btnSave" onclick="checkfile(document.getElementById('fileAuthorization'))" ></button> 

,當你準備提交您可以用JavaScript在回調函數做這樣的形式,它不會提交表單自動

document.getElementById('form-id').submit(); 
+0

是的我試過這個,如果我改變'提交按鈕到按鈕'整個表單提交不會發生。在我的行動結果我將獲得文件屬性爲null。因爲在MVC我的行動結果我使用像這樣'HttpPostedFileBase文件= Request.Files [「fileAuthorization」]; MVC表單Submition是必要的這個上傳得到它的工作。 – RajeshKdev

+0

爲什麼在'onclick'屬性中有'javascript:'?處理表單的'submit'事件可能更好/更容易,而不僅僅是按鈕的事件。而使用JavaScript處理事件可能更好,而不是內聯HTML。 – Ian

+0

@lan請參閱我上面的評論 – RajeshKdev