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?
-
I have tried List, IEnumerable and an Array. I have also tried JSON.stringify. Nothing workedgriegs– griegs2013年06月06日 05:17:35 +00:00Commented Jun 6, 2013 at 5:17
2 Answers 2
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)
});
6 Comments
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'}
]
}