2014-01-11 260 views
2
**Jquery Ajax call sending data to controller** 

$("#btncareerssubmimt").click(function() { 
     var Name = $("#txtname").val(); 
var file = $("#fileresume").val(); 
$.ajax({ 
       type: "POST", 
       url: '/Footer/sendmail',    
       data: { Name: Name ,file: file }, 
       success: function() { 

        $("#simplecareers").html('<p style="color:#74ac12;margin-left:19%;">Your Request Submitted Successfully</p>'); 
       }, 
       error: function (XMLHttpRequest, textStatus, errorThrown) { 

        $("#simple-msg").html('<pre>"fail"</pre>'); 

       } 
      }); 
} 

在aboveo阿賈克斯CAL IAM得到正確名稱和文件路徑,但是,當我們傳遞給控制器​​ 變得名存實亡,但文件路徑越來越空 我的HTML視圖asp.net的MVC使用jQuery AJAX CAL HttpPostedFileBase文件中獲取空值

<input type="text" name="txtname" id="txtname" class="form-control" value=""/> 
input type="file" name="file" id="fileresume" value=""/> 
<input type="submit" value="Upload" id="btncareerssubmimt" class="submit" /> 


This is my controller 

    [HttpPost] 
     public ActionResult sendmail(string Name ,HttpPostedFileBase fileresume) 
     { 
} 
+0

檢查此問題http://stackoverflow.com/questions/166221/how-can-i-upload-files-asynchronously-with-jquery –

回答

2

您的HTML改爲:

<form id="formToSendEmail" action="/Footer/sendmail"> 
    <input type="text" name="Name" id="txtname" class="form-control" value=""/> 
    <input type="file" name="fileresume" id="fileresume" value=""/> 
    <input type="button" value="Upload" id="btncareerssubmimt" /> 
</form> 

和JS:

$("#btncareerssubmimt").click(function() { 
    $("#formToSendEmail").ajaxSubmit({ 
       type: "POST", 
       success: function() { 
        $("#simplecareers").html('<p style="color:#74ac12;margin-left:19%;">Your Request Submitted Successfully</p>'); 
       }, 
       error: function (XMLHttpRequest, textStatus, errorThrown) { 
        $("#simple-msg").html('<pre>"fail"</pre>'); 
       } 
      }); 
} 

.ajaxSubmit()是jQuery表單插件的方法,它幫助管理表單提交過程 - jQuery Form Plugin

+0

感謝您的回放,我試過.ajaxSubmit()和但沒有用,當我把.ajaxSubmit()iam獲取Uncaught TypeError:對象函數(e,t){返回新的x.fn.init (e,t,r)}沒有方法'ajaxSubmit'這個錯誤 – user2377806

+0

我編輯了我的答案,現在更完整,更容易理解。 –

+0

我跟着你的代碼 - 當我點擊上傳按鈕亞姆獲取Uncaught TypeError:對象[對象對象]沒有方法'ajaxSubmit'在螢火蟲 – user2377806

相關問題