jqGrid的形成服务器验证和自定义错误消息 [英] JqGrid forms server validations and custom error messages

查看:351
本文介绍了jqGrid的形成服务器验证和自定义错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对此有EDITTING添加和删除记录按钮工具栏编辑网格。

I have editable grid with toolbar that has buttons for editting adding and deleting records.


  1. 我想使用服务器端验证从我asp.mvc显示的jqGrid编辑表单验证消息。 (这是更多钞票?)

  2. 我想编辑表格上的信息将覆盖(内部服务器错误...)异常时在应用程序中发生。 (这应该是可能的,但我只是无法弄清楚如何做到这一点,可能使用 errorTextFormat ,但如何?)

  1. I would like to use server side validations from my asp.mvc to display validation messages on jqgrid edit form. (is this posible?)
  2. I would like to overide message (Internal server Error ...) on edit form when exception occurs in the application. (this should be possible but I just cant figure out how to do this, perhaps using errorTextFormat, but how? )

有人可以提供的例子吗?

Can someone provide example?

推荐答案

您是对的的 errorTextFormat 是获得在HTTP错误的情况下,服务器响应,并显示相应的错误信息的正确方法。

You are right that errorTextFormat is the correct way to get server response in case of HTTP errors and display the corresponding error message.

所有服务器首先必须返回响应与 HTTP在HTTP头错误code 。然后,你应该定义你的 errorTextFormat 事件处理的实施无论是作为 prmEdit prmAdd prmDel 参数的一部分 navGrid 或可以覆盖jqGrid的默认设置(看<一个href=\"http://stackoverflow.com/questions/5032327/jqgrid-navigator-how-to-specyfy-settings-globally/5032879#5032879\">here).我个人preFER通过修改设置 errorTextFormat jQuery.jgrid.edit jQuery.jgrid.del c可以对应$ C $的例子发现在以下<一href=\"http://stackoverflow.com/questions/2849189/display-nice-error-message-when-there-is-something-wrong-after-ajax-request-jqgri/2850524#2850524\">old回答。

First of all your server have to return response with an HTTP error code in the HTTP header. Then you should define your implementation of the errorTextFormat event handle either as a part of prmEdit, prmAdd, prmDel parameters of the navGrid or you can overwrite jqGrid default settings (see here). I personally prefer to set errorTextFormat by modifying of jQuery.jgrid.edit and jQuery.jgrid.del. An example of the corresponding code you can find in the following old answer.

的确切code errorTextFormat 函数应依赖于服务器的响应的格式。我使用ASP.NET MVC与WFC的网站内,服务器可以连接codeD字符串响应返回或者JSON(如果从来错误抛出新WebFaultException&LT;串&GT;(我的错误文本 ,状态code); 我明确地抛出)或某个HTML响应。我在执行 errorTextFormat 我测试哪种错误的响应我收到并转换服务器的响应。这里是code片段:

The exact code of errorTextFormat function should depend on the format of the server response. I use ASP.NET MVC with WFC inside of the site and the server can return either JSON encoded string response (if the error come from throw new WebFaultException<string> ("my error text", statusCode); which I thrown explicitly) or sometime HTML response. In my implementation of errorTextFormat I test which kind of error response I received and convert the server response. Here is the code fragment:

my.errorTextFormat = function (data) {
    var str = data.responseText.substr(0, 1);
    var str1 = data.responseText.substr(0, 6).toLowerCase();
    if (str === '"') {
        var errorDetail = jQuery.parseJSON(data.responseText);
        var s = "Fehler: '";
        s += data.statusText;
        s += "'. Details: ";
        s += errorDetail;
        return s;
    }
    else if (str1 === "<html " || str1 == "<html>" ||
             data.responseText.substr(0, 169) === '<?xml version="1.0" encoding="utf-8"?>\r\n<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\r\n<html ') {
        var bodyHtml = /<body.*?>([\s\S]*)<\/body>/.exec(data.responseText)[1];
        return bodyHtml; //bodyContents1;
    }
    else {
        var res = "Status: '";
        res += data.statusText;
        res += "'. Felhercode: ";
        res += data.status;
        return res;
    }
};
jQuery.extend(jQuery.jgrid.edit, {
    ...
    errorTextFormat: my.errorTextFormat
});
jQuery.extend(jQuery.jgrid.del, {
    ...
    errorTextFormat: Testportal.errorTextFormat
});

在code不是完美的,但你可以用它来创建自己的。

The code is not perfect, but you can use it to create your own one.

这篇关于jqGrid的形成服务器验证和自定义错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆