Join the Stack Overflow Community
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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.

share|improve this question
    
Try data: people instead of data: { people: people }. – haim770 Oct 28 '15 at 11:14
    
hi @haim770 this is the small example, i use this because i send more than one object, all of them have the same problem – TiGreX Oct 28 '15 at 11:15
    
@TiGreX try this public string TestPeople([FromBody] Person[] people) – Nasreddine Oct 28 '15 at 11:16
    
@Nasreddine , I still having the same result. – TiGreX Oct 28 '15 at 11:20
1  
@TiGreX Can you show how JSON you are sending looks like? – Kamo Oct 28 '15 at 11:27
up vote 1 down vote accepted

Try with:

data : JSON.stringify(peopleArray)

In your controller try:

public string Get(Person[] people){
   //some code
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.