Obtain a JSON object, sent as an ajax parameter, with asp.net

1

This is my js.

$('#tablaDemo').DataTable({
            processing: true,
            serverSide: true,
            ajax: {
                type: "POST",
                contentType: "application/json; charset=utf-8",
                url: "../Paginas/Crmedu_LstCliente.aspx/getDemo",
                data: function (d) {
                    console.log(d);
                    return JSON.stringify({ parameters: d , dato: "{value: 'Vitmar'}", midat: "Hola mundo"});
                }
            }
        });

and this is my method.

public static void getDemo(object parameters, object dato, string midat)
    {
       log.Info(String.Format("FORMAT REQ ========== >>>"+midat)); 
       //Request
       //var req = DataTableParameters.Get(parameters);
}

as I collect in value of dato , without having to create models or something like that, forget about the parameters.

for example midat if the value arrives. But from the json?

    
asked by Vitmar Aliaga 08.11.2016 в 18:42
source

2 answers

1

Asp.Net does a great job mapping serialized JSON parameters into .NET types, so it is unnecessary to double-serialize your JSON object by calling JSON.stringify

.NET already correctly performs data mapping by convention.

To access the data of a JSON object:

var data = {key1: 'value1', key2: 'value2'};

In your getDemo method you would declare the parameter as Dictionary<string, object> data

That way you can access its properties:

string key1 = data["key1"].ToString();

More useful info in the following link

    
answered by 08.11.2016 / 19:24
source
1

It's like david_rprada, the only thing I would add is that I recommend you to create specific types to receive in your controllers and not just objects, .NET will be responsible for de-serializing them correctly for you.

    
answered by 10.11.2016 в 19:15