首页 文章

如何只在MVC3中的Ajax.BeginForm的OnFailure中显示错误消息?

提问于
浏览
11

我在我的MVC3应用程序中有这个简单的ajax表单,它发送电子邮件消息:

@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(error){} javascript函数中显示:

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

问题是:error.responseText是 html dump of the entire error page.

I need to display only the exception error message (无论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("sender@gmail.com");
        mail.Subject = "Some Message";
        mail.Body = message;
        mail.To.Add("recepient@gmail.com");
        SmtpServer.Send(mail);
        return null;
    }

3 回答

  • 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("sender@gmail.com");
            mail.Subject = "Some Message";
            mail.Body = message;
            mail.To.Add("recepient@gmail.com");
            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);
        }
    

    希望这会帮助你 .

  • 2

    为了解决这个问题,我使用了HttpStatusCodeResult类 . 这是我参考的代码:

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

    这应该仍然会触发客户端上的所有正常错误行为,包括在浏览器控制台中输入500错误,并且应该与您已经用于显示错误的脚本完美配合 .

  • 0

    您可以在控制器中捕获异常并发回确切的错误消息 . 就像是:

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

    在这种情况下,您可能需要检查响应并相应地更新您的javascript showError .

相关问题