i’m trying to build a select element in the form editing jqgrid by calling an ajax webmethod (asp.net).
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:
this is an extract of the 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>";
}
}
},
...
where can i set the parameter to send to the GetCityByState webmethod?
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 🙁
I think you need ajaxSelectOptions parameter of jqGrid. For example if you need to have the id of the selected row as an additional
idparameter sent to webmethod identified bydataUrlyou can usedataparameter ofajaxSelectOptionsin function form:because in the code above the parameter
dataType: "json"are used you should modify the first line ofbuildSelectfromto
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 declarestringas the return type of the webmethod. They makes JSON serialization manually with call ofDataContractJsonSerializerorJavaScriptSerializer. 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 usedataType: "json"inside ofajaxSelectOptionsand 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$.parseJSONat all. You can just use directlydata.d: