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
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'));
}
}
}
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);
...
to
buildSelect: function (data) {
var response = $.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;
...