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.

11
  • Try data: people instead of data: { people: people }. Commented Oct 28, 2015 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 Commented Oct 28, 2015 at 11:15
  • @TiGreX try this public string TestPeople([FromBody] Person[] people) Commented Oct 28, 2015 at 11:16
  • @Nasreddine , I still having the same result. Commented Oct 28, 2015 at 11:20
  • 1
    @TiGreX Can you show how JSON you are sending looks like? Commented Oct 28, 2015 at 11:27

1 Answer 1

1

Try with:

data : JSON.stringify(peopleArray)

In your controller try:

public string Get(Person[] people){
   //some code
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.