0
votes

I'm trying to send a json object (extjs client ) to asp.net server side application:

This is the request (with sample data but same logic):

var datiOrdine = [];
for (i = 0; i < elencoChk.length; i++) 
{
    datiOrdine.push
                ({
                    x:  arr[i].value1, // string
                    y:  arr[i].value2, // string
                    z:  arr[i].value3, // string
                    k:  arr[i].value4, // string (dd/MM/yyyy)
                    l:  arr[i].value5, // double
                });
}
Ext.Ajax.request({
    url: '/RAMMVC/RDA/UpdateRDA',
    params: { array: Ext.JSON.encode(datiOrdine) },
    headers: { 'Content-Type': 'application/json; charset=utf-8' },
    method: "POST",
    success: function (response) {}, 
    failure: function (response) {}                 
});

The json object:

[  
  {  
     "x":"GRT02",
     "y":"0215000050",
     "z":"0001",
     "k":"30/01/2015",
     "l":1413.5
  }
]

And this is the method I declare on server side web application:

 public ActionResult UpdateRDA(?)
 {

 }

How can I declare the param(?) on UpdateRDA method to receive correctly the json object? Missing any annotations?

If I declare

 public ActionResult UpdateRDA(string array)
 {
 }

I get Invalid Json Primitives exception.

Programming languages:

client UI: extjs 4.1
server: asp.net MVC 3, net framework 4.0

2

2 Answers

1
votes

I was able to get your example working with a few small changes on similar setup (MVC3, ExtJS 4.2).

For your Ajax request you don't need 'method' as 'post' will be the default when parameters are present. Also, I am not familiar with the 'headers' property, I only know of 'defaultHeaders'. In any case you should not have the headers property set.

Here's what I used as a test case:

var datiOrdine = [];

for (i = 0; i < 10; i++) {
    datiOrdine.push({
        x: "GRT02", // string
        y: "0215000050", // string
        z: "0001", // string
        k: "30/01/2015", // string (dd/MM/yyyy)
        l: 1413.5, // double
    });
}

Ext.Ajax.request({
    url: '/myserver/Controller/Method',
    params: {
        array: Ext.JSON.encode(datiOrdine)
    },
    success: function (response) {
        alert('yes');
    },
    failure: function (response) { }
});

The server side method:

public ActionResult Test(string array)
{
    var test = array;
    return Content("");
}
0
votes
public ActionResult UpdateRDA(FormCollection collection)`
{
   // TODO
}