2013-04-18 80 views
10

我有這個簡單的Ajax形式在我MVC3應用程序,發送電子郵件:如何僅在MVC3中的Ajax.BeginForm OnFailure中顯示錯誤消息?

@using (Ajax.BeginForm("SendMessage", new AjaxOptions { 
    LoadingElementId = "wait", 
    OnSuccess = "loadMessages", 
    OnFailure = "showError" })) 
{ 
    <input type="text" name="message" placeholder="Message" /> 
    <input type="submit" name="submit" value="Send" /> 
} 

如果操作失敗發送消息時,它拋出異常。該異常被處理,並在showError(錯誤)顯示{} javascript函數:

function showError(error) { 
    $("#error").text(error.responseText); 
}; 

的問題是:error.responseText是整個錯誤頁面的HTML 轉儲。

我只需要顯示異常錯誤消息(無論在MVC3的ex.Message中)。

「public ActionResult SendMessage(string message){}」的實現是不相關的。

我需要知道如何僅在showError(錯誤)javascript處理程序中顯示異常消息。

謝謝你一堆。

[HttpPost] 
    public ActionResult SendMessage(string message) 
    { 
     MailMessage mail = new MailMessage(); 
     SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 
     mail.From = new MailAddress("[email protected]"); 
     mail.Subject = "Some Message"; 
     mail.Body = message; 
     mail.To.Add("[email protected]"); 
     SmtpServer.Send(mail); 
     return null; 
    } 
+0

請顯示您的SendMessage操作... –

+0

不知道它與問題有什麼關係... 8 | – monstro

+0

b/c除非你想捕獲http錯誤,否則你不能使用onFailure。想要了解你如何返回錯誤 –

回答

0

您可以在控制器中捕獲您的異常併發回確切的錯誤消息。喜歡的東西:

[HttpPost] 
public ActionResult SendMessage(string message) 
{ 
    try 
    { 
     MailMessage mail = new MailMessage(); 
     SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 
     mail.From = new MailAddress("[email protected]"); 
     mail.Subject = "Some Message"; 
     mail.Body = message; 
     mail.To.Add("[email protected]"); 
     SmtpServer.Send(mail); 
     return null; 
    } 
    catch (e) 
    { 
     throw new HttpException(500, e.Message); 
    } 
} 

您可能需要檢查在這種情況下的響應,並相應更新JavaScript showError

+0

謝謝,但它沒有不同於我沒有嘗試捕獲 - 它也返回整個錯誤頁面的HTML轉儲。不確定Ajax.BeginForm助手的OnFailure參數是否可以通過僅獲取錯誤消息來處理異常。看起來不可能。只是奇怪,如果你總是從控制器得到錯誤的HTML轉儲......我不想有任何TRY/CATCH塊的目的,我只是想傳播和輸出系統異常,但只有錯誤消息,而不是html內容。 – monstro

6

您無法處理OnFailure方法中的每個異常。因爲如果響應狀態不在200範圍內,則調用OnFailure。因此,你可以處理你的情況是這樣的:

[HttpPost] 
    public ActionResult SendMessage(string message) 
    { 
     MailMessage mail = new MailMessage(); 
     ActionResult response = null; 
     SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com"); 
     mail.From = new MailAddress("[email protected]"); 
     mail.Subject = "Some Message"; 
     mail.Body = message; 
     mail.To.Add("[email protected]"); 
     try 
     { 
      SmtpServer.Send(mail); 
      response = Content("Success"); 
     } 
     catch (Exception ex) 
     { 
      response = Content(ex.Message); 
     } 
     return response; 

    } 

在這裏,在這個代碼,如果郵件發送成功以後,我返回的響應「成功」別的我已經回到實際的錯誤,並在JavaScript我處理這樣的這種反應:

function loadMessages(error) { 
    if (error == 'Success') { 
     alert("Mail Sent successfully"); 
    } 
    else { 
     $("#error").text(error); 
    } 

希望這會幫助你。

1

爲了解決這個問題,我使用了HttpStatusCodeResult類。這裏是我有參考代碼:

try 
{ 
    // Some code that can error 
} 
catch (Exception e) 
{ 
    return new HttpStatusCodeResult(HttpStatusCode.InternalServerError, e.Message); 
} 

// View 
OnFailure = "alert(error);" 

這仍然應該引起所有客戶端,包括把一個500錯誤在瀏覽器控制檯上的正常的錯誤行爲,並應與腳本,你已經很好地工作用來顯示你的錯誤。