jqgrid表单编辑editoptions选择ajax添加参数 [英] jqgrid form editing editoptions select ajax add parameter

查看:28
本文介绍了jqgrid表单编辑editoptions选择ajax添加参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过调用 ajax webmethod (asp.net) 在表单编辑 jqgrid 中构建一个选择元素.

i'm trying to build a select element in the form editing jqgrid by calling an ajax webmethod (asp.net).

如果我调用没有参数的方法,一切都会很好.如果我尝试调用期望字符串参数的 web 方法,它不起作用:

Everythings work great if I call a method without parameter. It doesn't work if I try to call a webmethod expecting a string parameter:

这是代码的摘录:

ajaxSelectOptions: { type: "POST", contentType: 'application/json; charset=utf-8', },
colNames: ['City', 'State'],
colModel: [
{ 
    name: 'City', 
    index: 'City', 
    align: "center", 
    width: 80, 
    searchoptions: { sopt: ['eq', 'ne', 'cn']} ,
    edittype: 'select',
    editable: true, 
    editrules: { required: true },
    editoptions: { 
        dataUrl: '<%# ResolveUrl("~/Service/Domain/ServiceGeographic.asmx/GetCityByState") %>',
        buildSelect: function (data) {
        var retValue = $.parseJSON(data);
        var response = $.parseJSON(retValue.d);

        var s = '<select id="customer_City" name="customer_City">';

        if (response && response.length) {
            for (var i = 0, l = response.length; i < l; i++) {
            s += '<option value="' + response[i]["Id"] + '">' + response[i]["Descrizione"] + '</option>';
            }
        }

        return s + "</select>";
        }                        
    }
},
...

我可以在哪里设置参数以发送到 GetCityByState 网络方法?

where can i set the parameter to send to the GetCityByState webmethod?

编辑:我没有强调我使用 POST 来调用 webmethod.即使我尝试了 Oleg 建议这个链接,它不起作用:(

EDIT: I did not highlight that I'm using POST to call webmethod. Even if i tried as Oleg suggested on this link, it doesn't work :(

推荐答案

我觉得你需要 ajaxSelectOptions jqGrid 的参数.例如,如果您需要将所选行的 id 作为附加 id 参数发送到由 dataUrl 标识的 webmethod,则可以使用 data 参数ajaxSelectOptions 的函数形式:

I think you need ajaxSelectOptions parameter of jqGrid. For example if you need to have the id of the selected row as an additional id parameter sent to webmethod identified by dataUrl you can use data parameter of ajaxSelectOptions in function form:

ajaxSelectOptions: {
    type: "GET", // one need allows GET in the webmethod (UseHttpGet = true)
    contentType: 'application/json; charset=utf-8',
    dataType: "json",
    cache: false,
    data: {
        id: function () {
            return JSON.stringify($("#list").jqGrid('getGridParam', 'selrow'));
        }
    }
}

因为在上面的代码中使用了参数dataType: "json",你应该修改buildSelect的第一行 from

because in the code above the parameter dataType: "json" are used you should modify the first line of buildSelect from

buildSelect: function (data) {
    var retValue = $.parseJSON(data);
    var response = $.parseJSON(retValue.d);
    ...

buildSelect: function (data) {
    var response = $.parseJSON(data.d);
    ...

此外,因为您使用行 $.parseJSON(data.d) 我可以假设您以错误的方式从 webmethod 返回数据.通常,来自 web 方法的返回值类型应该是 class.您不应该包含任何对返回对象的手动序列化调用.相反,有些人误解了这一点,并将 string 声明为 webmethod 的返回类型.他们通过调用 DataContractJsonSerializerJavaScriptSerializer 手动进行 JSON 序列化.结果,作为字符串返回的手动序列化数据将再序列化一次.这就是你可以有 两次 调用 $.parseJSON 的原因: var retValue = $.parseJSON(data);var response = $.parseJSON(retValue.d);.如果您将在 ajaxSelectOptions 中使用 dataType: "json" 并且如果您将在 web 方法中执行 no 手动序列化到 JSON 并且只是 rejurn像它这样的对象,您根本需要 no 调用 $.parseJSON .你可以直接使用data.d:

Moreover because you use the line $.parseJSON(data.d) I can suppose that you return the data from the webmethod in the wrong way. Typically the type of return value from the webmethod should be class. You should don't include any call of manual serialization of the returned object. Instead of that some people misunderstand that and declare string as the return type of the webmethod. They makes JSON serialization manually with call of DataContractJsonSerializer or JavaScriptSerializer. As the result the manual serialized data returned as string will be one more time serialized. It's the reason why you can have two calls of $.parseJSON: var retValue = $.parseJSON(data); var response = $.parseJSON(retValue.d);. If you will be use dataType: "json" inside of ajaxSelectOptions and if you would do no manual serialization to JSON in web method and just rejurn the object like it be, you would need to have no call of $.parseJSON at all. You can just use directly data.d:

buildSelect: function (data) {
    var response = data.d;
    ...

这篇关于jqgrid表单编辑editoptions选择ajax添加参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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