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

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

问题描述

我有一个带有工具栏的可编辑网格,其中包含用于编辑添加和删除记录的按钮.

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? )

有人可以举个例子吗?

推荐答案

你说得对 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 错误代码.然后你应该定义 errorTextFormat 事件的实现作为 navGrid 或者您可以覆盖 jqGrid 默认设置(参见 这里).我个人更喜欢通过修改 errorTextFormat 来设置 <代码>jQuery.jgrid.edit 和 jQuery.jgrid.del.您可以在以下 旧答案.

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.

errorTextFormat 函数的确切代码应该是取决于服务器响应的格式.我在站点内部使用带有 WFC 的 ASP.NET MVC,服务器可以返回 JSON 编码的字符串响应(如果错误来自 throw new WebFaultException ("my error text", statusCode); 我明确抛出)或有时是 HTML 响应.在我的 errorTextFormat 的实现中,我测试了哪种我收到的错误响应并转换服务器响应.这是代码片段:

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"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html ') {
        var bodyHtml = /<body.*?>([sS]*)</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
});

代码并不完美,但您可以使用它来创建自己的代码.

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

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

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