I have the following code where on button click I make a ajax call to c# controller. I also need to pass some values back to controller.
var array = JSON.stringify(myArrayList);
$.ajax({
type: "POST",
dataType: 'json',
data: {
'type': vm.myType,
'mod': vm.myMod,
'ar' : array
},
url: rootDir + "/Home/GetData",
success: function (data) {
console.log(data);
},
error: function (xhr, status, error) {
console.log("Error");
}
});
And in my c# controller I have:
[HttpPost]
public IActionResult GetData(string type, string mod, string ar)
{
//I can get type and mod but having issues in getting my array data
}
My Array data is as below:
[{"id":"1","name":"John"},{"id":"2","name":"Steve"}]
In my loop I want to grab is and name values. I tried with
JArray v = JArray.Parse(ar);
But cant loop though and get the values
Updated:
I think there is issue in my json array because of which I am not able to get the values of json array in my c# controller.
When I debug my array in java script it has values like this:
Array(2)
0 Object
id: "1"
name: "John"
1 Object
id: "2"
name: "Steve"
After I use Json.stringify as below:
var array = JSON.stringify(myArrayList);
Now in my C# controller I get the resultant string as:
[{"id":"1","name":"John"},{"id":"2","name":"Steve"}]
This I think is not correct because when I use the below code: dynamic stuff = JObject.Parse(ar);
It throws error:
'Error reading JObject from JsonReader. Current JsonReader item is not an object: