1

I am trying to send multiple objects in js to a controller in C# using an Ajax call.

I have a object in C# called "Person"

which is the next :

public class Person
{
    public string Title { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Age { get; set; }
}

and I have the same object In JavaScript.

Then create two persons and I send to the controller. This is the Ajax call

$.ajax({
    url: baseUrl + "/controller/TestPeople",
    type: "POST",
    data: {
        people: people
    },
    success: function (resp) {
        alert("ok");
    }
});

This is the post

people[0][Title]:"Mr."
people[0][FirstName]:"fname1"
people[0][LastName]:"Lname1"
people[0][Age]:23
people[1][Title]:"Mr."
people[1][FirstName]:"fname2"
people[1][LastName]:"Lname2"
people[1][Age]:25

but when i receive it in the controller, everything is null

public string TestPeople(Person[] people){
   //some code
}

the controller knows that there are 2 people but all the information inside is null.

Any idea why?

To "solve" the problem i change the controller to use FormCollection and it is working, but i would like to know why the other is not working.

Thanks for all.

Fayyaz Naqvi
  • 695
  • 7
  • 19
TiGreX
  • 1,602
  • 3
  • 26
  • 41

1 Answers1

1

Try with:

data : JSON.stringify(peopleArray)

In your controller try:

public string Get(Person[] people){
   //some code
}
Remo H. Jansen
  • 23,172
  • 11
  • 70
  • 93