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?

asked Jun 6, 2013 at 5:14
1
  • I have tried List, IEnumerable and an Array. I have also tried JSON.stringify. Nothing worked Commented Jun 6, 2013 at 5:17

2 Answers 2

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)
 });
answered Jun 6, 2013 at 5:20

6 Comments

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.
And I have already tried stringify and that didn't work either.
i think this link can help you
"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
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)); } });
|
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'}
 ]
}
answered Jun 6, 2013 at 5:24

1 Comment

i've tried hardcodeing the array data like you have here and the array has the correct element count but the data is empty

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.