1

I have the following json object, sorry for the image;

enter image description here

The jquery code I have looks like this;

var data = {
    table: table,
    favour: $("[name='radFavour']:checked").val(),
    data: jsonObj
};

$.ajax({
    url: appDomain + "/Compare/Ajax_Update",
    type: "post",
    dataType: "json",
    data: data
});

The c# code looks like;

[HttpPost]
public void Ajax_Update(CompareFVM fvm)
{
}

The FVM contains a string for table and for favour and the data for those two properties comes through.

For "data" I have the following in the FVM;

public List<CompareItem> data { get; set; }

And the item;

public class CompareItem
{
    public int prodId { get; set; }
    public int stageId { get; set; }
    public string value { get; set; }
    public string property { get; set; }
}

The List has the correct amount of elements in it, in this case two, but each of them has nulls set.

So the data I am posting back is not coming through for the array elements but it is for the single fields.

Any ideas?

Community
  • 1
  • 1
griegs
  • 22,624
  • 33
  • 128
  • 205
  • I have tried List, IEnumerable and an Array. I have also tried JSON.stringify. Nothing worked – griegs Jun 06 '13 at 05:17

2 Answers2

1

while ajax calling, pass the objectname as 'fvm'(name should be matching with the C# code parameter). also, please check passing json abject using JSON.stringify(data).

    var fvm = {
        table: table,
        favour: $("[name='radFavour']:checked").val(),
        data: jsonObj
    };

    $.ajax({
        url: appDomain + "/Compare/Ajax_Update",
        type: "post",
        dataType: "json",
        data: JSON.stringify(fvm)
    });
Paritosh
  • 11,144
  • 5
  • 56
  • 74
  • All the names match that that is being passed. Like I said, I am passing two objects back in "data". At the C# end, my FVM.data list has two objects. Both contain defaults and not the actual data. So it knows there are two objects, but the mapping is screwing up. – griegs Jun 06 '13 at 05:23
  • And I have already tried stringify and that didn't work either. – griegs Jun 06 '13 at 05:24
  • i think [this link](http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery/11736771#11736771) can help you – Paritosh Jun 06 '13 at 05:27
  • "Are you trying do a cross-domain AJAX call? Meaning, your service is not hosted in your same web application path? Your web-service must support method injection in order to do JSONP" - then this can help you – Paritosh Jun 06 '13 at 05:28
  • No there is no cors issue here. – griegs Jun 06 '13 at 05:32
  • 1
    not sure why you are facing the issue, I tried and its working for me.. the object is having simple structure. code: var composite = { BoolValue: true, StringValue: "Hello " }; $.ajax({ type: "POST", contentType: "application/json", url: url, data: JSON.stringify(composite), datatype: "jsonp", success: function (data) { alert(JSON.stringify(data)); }, error: function (xhr, status, error) { alert(JSON.stringify(xhr)); } }); – Paritosh Jun 06 '13 at 05:41
0

Just basing off what similar things I've done in the past, I'd structure your code like so:

// if your C# is
public void Ajax_Update(CompareFVM fvm) {
}

// then your ajax call should be along the lines of
$.ajax({
    data : {
        'data' : [
            { /* compareItem */ },
            { /* compareItem */ },
            // ...
        ]         
    }
})

The thing being, your C# endpoint is expecting an object, so you should give it a JSON object.

If your C# class is

public class CompareFVM {
    public IList<CompareItem> data { get;set; }
}

then your corresponding JSON should be:

{ 'data' : [] }

where .data would be an array of CompareItem objects.

e.g.

{ 
    'data' : [
        {'prodId':'3175','stageId':'19045','value':'TUE','property':'despatchDay'},
        {'prodId':'3175','stageId':'19045','value':'TUE','property':'despatchDay'}
    ]
}
Richard Neil Ilagan
  • 14,627
  • 5
  • 48
  • 66
  • i've tried hardcodeing the array data like you have here and the array has the correct element count but the data is empty – griegs Jun 06 '13 at 05:39