0

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: 
1
  • see my updated question. Looks like my array is not constructing right. Is there some issue in how i send data to my controller Commented Oct 12, 2017 at 12:44

2 Answers 2

3

You need to cast to the correct type to enumerate:

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

string ar = "[{\"id\":\"1\",\"name\":\"John\"},{\"id\":\"2\",\"name\":\"Steve\"}]";

var obj = JsonConvert.DeserializeObject(ar);

foreach (var item in ((JArray)obj))
{
    Console.WriteLine(
        string.Format("id:{0}, name:{1}",
                        item.Value<int>("id"),
                        item.Value<string>("name")));
}

output

id:1, name:John
id:2, name:Steve

But I would really recommend synchronizing your front and back end data models

public class Person
{
    [JsonProperty(PropertyName = "id")]
    public int Id { get; set; }

    [JsonProperty(PropertyName = "name")]
    public string Name { get; set; }
}

var obj = JsonConvert.DeserializeObject<List<Person>>(ar);

foreach (var person in obj)
{
    Console.WriteLine(
        string.Format("id:{0}, name:{1}",
                        person.Id,
                        person.Name));
}

output

id:1, name:John
id:2, name:Steve
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much. That did it. I have also updated my code to use proper data models.
0

Since JArray is Newtonsoft, you can use something like this to deserialize and then loop through the results:

var m_json = JsonConvert.DeserializeObject(ar);

foreach (var item in m_json)
{
  Console.Write(item.name);
}

1 Comment

This throw error: CS1579 foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.