【问题标题】:Exception message to UI到 UI 的异常消息
【发布时间】:2010-01-15 17:06:03
【问题描述】:

我打开一个 jQuery 对话框,在这个框中我进行了保存/取消。为了保存,我调用我的控制器,进行一些验证,保存或抛出异常 (MyPersonalException)。如果有异常,我会返回另一个视图(“MessageError”视图)以显示在弹出窗口中。我只想在模式框中看到“MyPersonalException”中可用的消息

我的问题: 1. 可以,但只适用于 Firefox 而不是 IE 不是 Chrome 2. 有没有其他方法,因为看起来很长的代码来显示一条消息。

控制器如下所示:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveOrUpdate(Guid id, string firstName, string LastName)
{
    try
    {
        Employee employee = new Employee() { Id = id, FirstName = firstName, LastName = LastName };
        _employeeService.SaveOrUpdate(employee);
        return Index();
    }
    catch (MyPersonalException ex)
    {
        _model.ErrorMessage = ex.Message;
        return View("MessageError", _model);

    }
    catch (Exception ex)
    {
        _model.ErrorMessage = ex.Message;
        return View("MessageError", _model);
    }
}

要调用对话框,我使用这段代码

jQuery(document).ready(function() { $(函数(){ /* 变量名 = $("#firstName"), 电子邮件 = $("#lastName"), 密码 = $("#isActive"), allFields = $([]).add(name).add(email).add(password), 提示 = $("#validateTips");*/

    $("#dialog").dialog({
        bgiframe: true,
        autoOpen: false,
        modal: true,
        buttons: {
            Save: function() {
                $.ajax({
                    type: "POST",
                    url: "/Employee/SaveOrUpdate",
                    data: {
                        id: getId(),
                        firstName: getFirstName(),
                        lastName: getLastName()
                    },
                    success: function(data) {
                        if (jqueryShowResult(data))
                            $("#DisplayError").html(data);
                        else {
                            employeeId = 0;
                            $(this).dialog('close');                               
                        }
                    },
                    error: function(XMLHttpRequest, textStatus, errorThrown) {
                    }
                })

            },
            Cancel: function() {
                employeeId = 0;
                $(this).dialog('close');
            }
        },
        close: function() {
            $("#gridEmpoyee").trigger("reloadGrid");
        },
        open: function() {
            $.ajax({
                type: "POST",
                url: "/Employee/GetEmployee",
                data: {
                    id: employeeId
                },
                success: function(data) {
                    $("#employeeDetail").html(data);
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                }
            })
        }
    });
});

});

jQueryShowResult

<script type="text/javascript">
    jqueryShowResult = function(msg) {
        var browser;
        try //Internet Explorer
                    {
            xmlDocTest = new ActiveXObject("Microsoft.XMLDOM");
            browser = "IE";
        }
        catch (e) {
            browser = "FF";
        }

        if (browser == "IE") {
            try {
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                xmlDoc.async = "false";
                xmlDoc.loadXML(msg);
                var message = xmlDoc.getElementsByTagName("message")[0].childNodes[0].nodeValue;
                var code = xmlDoc.getElementsByTagName("code")[0].childNodes[0].nodeValue;

                return false;
            }
            catch (e) {
                return true;
            }
        }
        else {

            var code = $(msg).find('code').text();
            var message = $(msg).find('message').text();
            if (code == "500") {
                return false;
            }
            else {
                return true;
            }
        }
    }; 
 </script>

【问题讨论】:

  • 我更新了下面的答案,这应该可以让你做你想做的事,而无需自定义 jQuery ajax 对象。

标签: jquery asp.net-mvc exception jquery-ui


【解决方案1】:

更新:抱歉,我们使用自定义包装器。 jQuery 默认不包含成功的 xmlHttpRequest。下面是另一种方法,它完全不会改变你的观点。您基本上只是在响应中检查带有id='code' 的元素,如果存在,则显示错误。

success: function(data, textStatus) {
  if ($("#code",data).length) { //See if the element <whatever id='code'> exists
    $("#DisplayError").html(data);
  } else {
    employeeId = 0;
    $(this).dialog('close');                               
  }
},

这是 jQuery 1.4 版本(参见 changes here,注意 Success 回调接收 XHR 对象作为第三个参数):

首先,在您的视图context.HttpContext.Response.StatusCode = 210; 中将StatusCode 设置为210,然后使用这种回调格式:

success: function(data, textStatus, xhr) {
  if (xhr.status == 210) {
    $("#DisplayError").html(data);
  } else {
    employeeId = 0;
    $(this).dialog('close');                               
  }
},

【讨论】:

  • 那我还得用“jqueryShowResult”吗?它适用于 FF、IE、Chrome 吗?
  • 这个解决方案不需要jQueryShowResult,它应该适用于所有浏览器,你只是在ajax结果中寻找一个带有id='code'的元素,这就是,data所做的在选择器上。
  • 也许你可以给我们你的客户包装:)
  • 其实你不需要它...这不是一个不常见的需求,是为 jQuery 1.4 添加的:api.jquery.com/jQuery.ajax 当你从 1.3.2 更改为 1.4 时,你可以使用以前的方法我发布了,点击编辑旁边的链接查看之前的答案并走那条路线。
  • @Kris-I Answer 已更新以包含两者,请查看我包含的 jQuery 更改链接以查看其他 1.4 内容,包括对成功回调更改的简短描述。
猜你喜欢
  • 2020-03-12
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-29
  • 2021-03-19
  • 2010-12-15
  • 2012-01-26
相关资源
最近更新 更多