2015-10-13 223 views
2

我想使用JQuery Ajax提交表單(Html.BeginForm())。 根據this question,它應該工作! 我不明白爲什麼來自動作SendEmail()的參數'email'沒有從js獲取值。 你能幫我嗎?ASP.Net MVC,使用JQuery.Ajax提交表單

筆者認爲:

<% using (Html.BeginForm("SendEmail", null, FormMethod.Post, new { @class = "form", @id = "formSendMail" })) 
{ %> 
<fieldset> 
    <ul> 
     <li> 
      <label for="MailFrom"> 
       De...</label> 
      <%= Html.TextBox("MailFrom", Session["email"].ToString(), new { @id = "MailFrom", @Name = "MailFrom", @readonly = "readonly" })%> 
     </li> 
     <li> 
      <label for="MailTo"> 
       A...</label> 
      <%= Html.TextBoxFor(m => Model.Agent.Email, new { @id = "MailTo", @Name = "MailTo" })%> 
     </li> 
     <li> 
      <label for="MailSubject"> 
       Objet :</label> 
      <%= Html.TextBoxFor(m => Model.MailSubject, new { @id = "MailSubject", @Name = "MailSubject" })%> 
     </li> 
     <li> 
      <label>&nbsp;</label> 
      <%= Html.TextArea("MailBody", Model.MailBody, 5, 10, null)%> 
     </li> 
    </ul> 
</fieldset> 
<% } %> 

我的控制器:

[HttpPost] 
public ActionResult SendEmail(Email email) 
{ 
    if (email != null) 
    { 
     if (!string.IsNullOrEmpty(email.MailBody) & !string.IsNullOrEmpty(email.Subject) & !string.IsNullOrEmpty(email.To)) 
     { 
      using (IEmailDal emailDal = new EmailDal()) 
      { 
       emailDal.SendEmail(email); 
      } 

      return Json("Email envoyé", JsonRequestBehavior.AllowGet); 
     } 
     else 
      return Json("Error"); 
    } 
    else 
     return Json("Error"); 
} 

我的電子郵件類:

public class Email 
{ 
    public string From { get; set; } 
    public string To { get; set; } 
    public string Subject { get; set; } 
    public string MailBody { get; set; } 
} 

要提交我的形式,我通過jQuery的一個按鈕模擬提交動作.dialog:

$("#mail-form").dialog({ 
    buttons: { 
     "Envoyer le mail": function() { 
      $("#formSendMail").submit(); 
     } 
    } 
}); 

而且我的javascript:

$('#formSendMail').submit(function (e) { 
    var myEmail = { 
     From: $('#MailFrom').val(), 
     To: $('#MailTo').val(), 
     Subject: $('#MailSubject').val(), 
     MailBody: $('#MailBody').val() 
    }; 

    $.ajax({ 
     type: "POST", 
     url: '<%= Url.Action("SendEmail", "Messages") %>', 
     data: JSON.stringify(myEmail), 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: function (result) { 
      alert("Mail envoyé."); 
     }, 
     error: function (result) { 
      alert("Echec lors de l'envoi du mail."); 
     } 
    }); 

    return false; 
}); 

謝謝!

+1

你的表單沒有提交按鈕 – Luke

+2

而作爲一個側面說明 - 這個問題的答案是並不完全正確 - 你不需要字符串化的數據,如果你刪除'的contentType:「應用/ JSON的;字符集= utf-8「,' –

+0

@Coulton:它沒有提交按鈕,因爲我使用的是JQuery.Dialog,我通過對話框中的按鈕模擬提交方法:$(」#mail-form「)。對話框({ 的AutoOpen:假的, 寬度:1140, 模式:真, 按鈕:{ 「Envoyer樂郵件」:函數(){ $( 「#formSendMail」)提交();} 。} }); – Flesym

回答

0

下面是答案(感謝斯蒂芬·馬克):

我只需要刪除contentType: "application/json; charset=utf-8",,則無需字符串化數據:

$.ajax({ 
    type: "POST", 
    url: '<%= Url.Action("SendEmail", "Messages") %>', 
    data: myEmail, 
    dataType: "json", 
    success: function (result) { 
     alert("Mail envoyé."); 
    }, 
    error: function (result) { 
     alert("Echec lors de l'envoi du mail."); 
    } 
}); 

現在,爲什麼沒有在工作第一名,我不知道!

0

如果action方法運行正確,你的問題就是URL的錯誤路徑。這是正確的URL。

url: '@Url.Action("SendEmail", "SampleController")', 

與稱爲SendEmail第一個參數是動作方法,第二個參數被稱爲SampleControl含有SendEmail方法。

0

爲什麼你要處理的,如果這是上彈出按鈕,然後直接處理它,我的意思是阿賈克斯直接發佈它,如果你是透過JSON.stringify然後在操作層面處理像這樣提交事件:

[HttpPost] 
public ActionResult SendEmail(string email) 
{ 
    Email formData = new JavaScriptSerializer().Deserialize<Email>(email); 
    //Now you have the object handle it how you want 
} 
0

請改變電子郵件類的屬性的名稱與您的文本框字段名稱的名稱相同,然後您將獲得控制器中的值。

public class Email 
{ 
    public string MailFrom { get; set; } 
    public string MailTo { get; set; } 
    public string MailSubject { get; set; } 
    public string MailBody { get; set; } 
}