UI的异常消息

Kri*_*s-I 5 asp.net-mvc jquery jquery-ui exception

我打开一个jQuery对话框,在这个框中我做了一个保存/取消.要保存,我调用我的控制器,进行一些验证,保存或抛出异常(MyPersonalException).如果有异常,我返回另一个View("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);
    }
}
Run Code Online (Sandbox Code Playgroud)

要调用该对话框,我使用此代码

jQuery(document).ready(function(){$(function(){/*var name = $("#firstName"),email = $("#lastName"),password = $("#isActive"), allFields = $([]).add(name).add(email).add(password),tips = $("#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) {
                }
            })
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

});

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>
Run Code Online (Sandbox Code Playgroud)

Nic*_*ver 2

更新:抱歉,我们使用自定义包装器。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');                               
  }
},
Run Code Online (Sandbox Code Playgroud)

这是 jQuery 1.4 版本(请参阅此处的更改,注意Success 回调接收 XHR 对象作为第三个参数):

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

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