0

In after inline edit free jqgrid sends variable number of columns to ASP.NET MVC4 WebAPI controller using POST method. Posted data contains json string below.

json string posted to controller in POST body looks like:

{"Toode":"","Kogus":"0.0000",
 "Nimetus":"1","Mootyhik0_nimetus":"",
 "Reanr":"2",

"_oper":"edit","_rowid":"1673",

"_dokdata":"[
   {\"name\":\"Maksetin1_tingimus\",\"value\":\"\"},  
   {\"name\":\"Kinnitatud\",\"value\":\"False\"}]"

}

It has 3 regions of properties:

  1. Order details. Dynamic properties "Toode":"","Kogus":"0.0000","Nimetus":"1","Mootyhik0_nimetus":"", "Reanr":"2" This list may change on runtime depending on user preferences.

This is created by using jqgrid extraparam: getDetails() setting.

  1. Fixed properties stating with underscore `"_oper":"edit","_rowid":" Those are _oper and _rowid are always present jqgrid adds them automatically.

  2. Order data. fixed property _dokdata which contains json array of dynamic properites {\"name\":\"Maksetin1_tingimus\",\"value\":\"\"},{\"name\":\"Kinnitatud\",\"value\":\"False\"}

This is created from jqgrid colmodel.

This list may change in runtime and may contain same poperty names as in p.1 order details.

How to get this data in controller ? Probably it shoud be parsed into two dictionaries . Is it possible to force WebAPI to pass this data as classes/collection to controller or should it parsed manually?

jqgrid settings are from answer Using jqGrid's inline-editing with RESTful urls? Those and serialize methods can changed if this makes data passing more reasonable. Resulting WebAPI should have easily understandable data format (good API) since it will be used by third party applications also.

    $.extend($.jgrid.defaults, {
        ajaxRowOptions: { contentType: "application/json", async: true },
        serializeRowData: function (data) {
            var propertyName, propertyValue, dataToSend = {};
            for (propertyName in data) {
                if (data.hasOwnProperty(propertyName)) {
                    propertyValue = data[propertyName];
                    if ($.isFunction(propertyValue)) {
                        dataToSend[propertyName] = propertyValue();
                    } else {
                        dataToSend[propertyName] = propertyValue;
                    }
                }
            }
            return JSON.stringify(dataToSend);
        }
    });

Update

serializeRowData is changed to

        serializeRowData: function (data) {
            return JSON.stringify({
                headerData: $("#_form").serializeArray(),
                rowData: data
            } );
        }

This produces bloated json for headerData:

{"headerData": [
{"name":"Tasudok","value":"134"},
{"name":"Kuupaev","value":"2015-11-23"},
{"name":"Dokumnr","value":"135319"}
],

"rowData": {"Toode":"",
 "Kogus":"0.0000",
 "Nimetus":"öäölä<a",
 "_rowsum":"0.00",
"Id":"1639",
"Dokumnr":"135319",
"_oper":"edit",
"_rowid":"1639"
}
}

How remove name and value properties from headerData so that it contains property name and value pairs like in rowData:

headerData: {
    "Tasudok": "134",
    "Kuupaev": "2015-11-23",
    "Dokumnr": "135319"
   }
Community
  • 1
  • 1
Andrus
  • 26,339
  • 60
  • 204
  • 378
  • It seems to me that it would be more easy if you would remove additional JSON encodding of `_dokdata`. You need just change the code of `getDetails` to use `return $("#_form").serializeArray();` instead of `return JSON.stringify($("#_form").serializeArray());`. As the result you should have **static** data where `_dokdata` is array/list of items which have two properties: `name` and `value`. You can encode the first part (`"Toode":"","Kogus":"0.0000", "Nimetus":"1","Mootyhik0_nimetus":"", "Reanr":"2"`) in the same way like `_dokdata`: array of `name` and `value` properties. – Oleg Nov 24 '15 at 22:19
  • You are welcome! I don't understand the format of data which you use and the data of your application in general. Odata plugin is good it you have good model classes in C# code (better the classes which present Data Model from Entity Framework) additionally you need include OData support on the server side. I'm not sure whether it's your case. What I meant before is the usage of `serializeRowData` for encode all other properties as `_oper`, `_rowid` and `_dokdata` in one array (`nameValues`) of items with `name` and `value` properties. You need just modify your current `serializeRowData`. – Oleg Nov 24 '15 at 23:27
  • The corresponding controller action could be `(MyParams p)` where class `MyParams` has 4 public properties: `List nameValues`, `string _oper`, `string _rowid` and `List _dokdata`. The class `MyNameValue` have just two string values: `name` and `value`. – Oleg Nov 24 '15 at 23:32
  • @Oleg: Application is used in lot of different servers in customer sites. Different columns can added to database tables at runtime in different sites. So fixed model and EF cannot used. Where do find dynamic OData parser for WebAPI ? I trid to create simple json for document header and row but encountered issues. I updated question. How to fix ? – Andrus Nov 25 '15 at 13:49
  • Probably you misunderstand how OData works. I recommend you to read [the article](http://www.odata.org/blog/how-to-use-web-api-odata-to-build-an-odata-v4-service-without-entity-framework/), which I personally find very good for understanding OData. It shows how one can add OData support to an **existing solution**. – Oleg Nov 25 '15 at 14:08
  • Sorry, I'm busy this week. If you can't solve the problem yourself that you can create **separate Visual Studio ASP.NET MVC project** with minimal functionality and simple dummy data (two items of static data). I could look inside after I'll get more time. About OData: I think that you still wrong interpret it. OData allows you first make separate request for building `colModel` based on the server data. Then all works like in standard jqGrid. No special "OData request for jqgrid Edit method" exist and could exist. – Oleg Nov 25 '15 at 16:38

0 Answers0