1

I wish to simply do an AJAX call to a method expecting a List of Key and value pairs, but I have no idea how to do this. I tried the following:

Server method:

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

data to be 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. (network tab showed it), but it did not work. I also tried to make an array of objects with one property (propertyname 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 receive an array/list of 3 items with two properties, but the properties are always empty. Does anyone know the correct format for the data to be send

Kai
  • 732
  • 1
  • 7
  • 23

2 Answers2

0

I think you can simply create your object like this:

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

Then make it as data parameter in your ajax request, receive it as a dictionary in your c# action.

Juxture
  • 253
  • 3
  • 15
AlameerAshraf
  • 872
  • 1
  • 13
  • 23
0

Use something like this.

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

And use the bracket notation:

Instead of brancheItems.push({ key : value }); use

var object = {}; 
object[key] = value;
brancheItems.push(object);
Murat Gündeş
  • 852
  • 2
  • 17
  • 31