Ajax calls an action with a list of KeyValuePairs as a parameter

I want to just make an AJAX call for a method that expects key pairs and values, but I have no idea how to do this. I tried the following:

Server Method:

UpdateBranches(List<KeyValuePair<string, string>> brancheItems)

data to send:

 var brancheItems = [];
 businessActivities.forEach(f =>   
     brancheItems.push({
      Key: f.sbiCode,
      Value: f.sbiCodeDescription
 })

This seemed to give me an array of objects with key and value properties. (showed the network tab), but it did not work. I also tried to create an array of objects with one property (the name of the property is the key, value is the value):

for (var itemIndex in items[index].businessActivities) {
   var key = items[index].businessActivities[itemIndex].sbiCode;
   brancheItems.push({ 
key: items[index].businessActivities[itemIndex].sbiCodeDescription
        });
    }

Note that on the server, I seem to get an array / list of 3 elements with two properties, but the properties are always empty. Does anyone know the correct format for sending data

+4
source share
2 answers

, :

var ParamtersContainer = new Object();
ParamtersContainer = {
    "PatientName": 'Alameer',
    "ServiceDate": '12/12/2017',
    "ProviderName": 'ahmed'
};

ajax-, #.

0

- .

    $.ajax({
        type: 'POST',
        url: url,
        contentType: "application/json",
        data:JSON.stringify( {brancheItems: brancheItems}),
        success: function (data) {
            alert("Succeded");
        }
    });

bracket:

brancheItems.push({ key : value });

var object = {}; 
object[key] = value;
brancheItems.push(object);
0

Source: https://habr.com/ru/post/1688088/


All Articles