1

I have an AJAX-enabled WCF service with the following signature:

       [OperationContract]
       [WebGet]
       public JQGridContract GetJQGrid(int entityIndex)

And the following data contract:

[DataContract]
public class JQGridContract
{
    [DataContract]
    public class Row
    {
        [DataMember]
        public int id { get; set; }

        [DataMember]
        public List<string> cell { get; set; }

        public Row()
        {
            cell = new List<string>();
        }
    }

    [DataMember]
    public int page { get; set; }

    [DataMember]
    public int total { get; set; }

    [DataMember]
    public int records { get; set; }

    [DataMember]
    public List<Row> rows { get; set; }

    public JQGridContract()
    {
        rows = new List<Row>();
    }
}  

Basically I need to change the postData of the client-side jqGrid to send 'entityIndex' to this service.

I've read how its supposed to function and from what I can tell this should work:

 function loadGrid() {

    $("#jqGrid").jqGrid({

        postData: { entityIndex : function () {    // modify the data posted to AJAX call here

            return 6;   

          })
        },
        gridComplete: function () {

            $("#jqGrid").setGridParam({ datatype: 'local' });
        },
        datatype: function (pdata) {
            getData(pdata);
        },

And here is the getData() function:

  function getData(pdata) {

    var params = new Object();

    alert(pdata.entityIndex());               // this displays '6', correctly

    params.entityIndex = pdata.entityIndex(); 


    $.ajax(
            {
                type: "GET",
                contentType: "application/json; charset=utf-8",
                url: "AJAXService.svc/GetJQGrid",
                data: JSON.stringify(params),
                dataType: "json",
                success: function (data, textStatus) {
                    if (textStatus == "success") {
                        var thegrid = $("#jqGrid")[0];

                        thegrid.addJSONData(data.d);
                    }
                },
                error: function (data, textStatus) {
                    alert('An error has occured retrieving data!');
                }
            });

Ive confirmed the following in Firebug:

1) The json params are correct : {"entityIndex":6}

2) The AJAX service returns JSON data to the grid, its just the wrong data

And here is the wierd part:

I logged the 'entityIndex' thats actually working inside the WCF operation -- and its ALWAYS coming up as 0?

Thanks.

Sean Thoman
  • 7,429
  • 6
  • 56
  • 103

2 Answers2

1

I will not criticize the style of your program. I could write too many things about this. :-)

You current main problem could be solved with the usage JSON.stringify(pdata.entityIndex()) instead of JSON.stringify(params) or with the usage of another BodyStyle of the WFC method (see here for details)

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Is the style problem on the WCF side or the js side? I threw this together rather quickly, trying to understand the basics. Ultimately I will want the jqGrid postData to be based on whatever is in the url's queryString. – Sean Thoman Jun 14 '11 at 19:01
  • Neither of those options seem to work. First of all calling pdata.entityIndex() just returns 6, not { "entityIndex": 6 }. If I change it to return { "entityIndex" : 6} I am back where I started. Like I said I inspected in firebug the 'params' tab and it shows: { "entityIndex" : 6 }. Regardless of the BodyStyle on the WCF operation the actual value that gets passed to the method is always 0. – Sean Thoman Jun 14 '11 at 19:26
  • @Sean: If you post an URL of your demo project I could try to find why my suggestion not work for you. About the style I mean the client part only. I find the usage of `datatype` as function a wrong way. Look at one [old answer](http://stackoverflow.com/questions/3912008/jqgrid-does-not-populate-with-data/3914796#3914796) where you can download the full [demo project](http://www.ok-soft-gmbh.com/jqGrid/WfcToJqGrid.zip). Implementing paging,sorting and filtering you will find [here](http://stackoverflow.com/questions/5500805/asp-net-mvc-2-0-implementation-of-searching-in-jqgrid/5501644#5501644) – Oleg Jun 14 '11 at 19:42
  • @Sean: Additionally about the style... I wrote you in one of the previous comment that the setting of `$("#jqGrid").setGridParam({ datatype: 'local' });` in the `gridComplete` is **the same** as the usage of `loadonce:true`. So why you use the complex construct instead of the simple jqGrid parameter? – Oleg Jun 14 '11 at 19:49
0

I got it working, it is close to what Oleg said, just that you do not need to do JSON.stringify.

If you have WebMessageBodyStyle.WrappedRequest, this works:

data: { entityIndex: pdata.entityIndex() },   

OR if you have no BodyStyle, this works:

data: { "entityIndex": pdata.entityIndex() },  
Sean Thoman
  • 7,429
  • 6
  • 56
  • 103
  • I supposed that you configured the JSON format of the input and output data in the web.config. – Oleg Jun 14 '11 at 19:46