jqGrid的形式编辑editoptions选择添加AJAX参数 [英] jqgrid form editing editoptions select ajax add parameter

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

问题描述

我试图通过调用一个ajax的WebMethod(asp.net)建的形式编辑的jqGrid一个select元素。

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

万物工作的伟大,如果我调用一个方法没有参数。如果我尝试调用一个WebMethod期待一个字符串参数它不工作:

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:

这是code的摘录:

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的webmethod?

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

修改:我没有突出,我使用POST调用的WebMethod。即使我试图为<一个href=\"http://stackoverflow.com/questions/4469650/jqgrid-incorrect-select-drop-down-option-values-in-edit-box/4480184#4480184\">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确定您可以使用数据函数形式 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'));
        }
    }
}

因为在code居留权参数数据类型:JSON用于你应该修改的第一行 buildSelect >

because in the code abode 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以错误的方式返回数据。通常情况下返回值从WebMethod的类型应该是的的。你应该不包括返回对象的手动序列化的任何电话。相反的,有些人误会,并声明字符串作为的WebMethod的返回类型。他们手工做JSON序列化与 DataContractJsonSerializer 的JavaScriptSerializer 的电话。其结果是手动序列化的数据返回的字符串将是一个更多的时间序列。这就是为什么你可以有原因的两个 $ parseJSON 的呼叫: VAR retValue = $ .parseJSON(数据); VAR响应= $ .parseJSON(retValue.d); 。如果你会使用数据类型:JSON ajaxSelectOptions 内,如果你会做的无需手动序列化以JSON在Web方法并只rejurn喜欢它的对象是,你需要有 $。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天全站免登陆